rust/clippy_lints/src/unnecessary_self_imports.rs
xFrednet d647696c1f
Added clippy::version attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...

And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...

Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.

```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;

let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
    ls | where name =~ "rust-" | select name | format {name}/lints.json |
    each { open $it | select id | insert version $it | str substring "5,11" version} |
    group-by id | rotate counter-clockwise id version |
    update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
    let version = ($versions | where id == ($row.id) | format {version})
    let idu = ($row.id | str upcase)
    $"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n    pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```

And this still has some problems, but at this point I just want to be done -.-
2021-11-10 19:48:31 +01:00

71 lines
2.7 KiB
Rust

use clippy_utils::diagnostics::span_lint_and_then;
use if_chain::if_chain;
use rustc_ast::{Item, ItemKind, UseTreeKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol::kw;
declare_clippy_lint! {
/// ### What it does
/// Checks for imports ending in `::{self}`.
///
/// ### Why is this bad?
/// In most cases, this can be written much more cleanly by omitting `::{self}`.
///
/// ### Known problems
/// Removing `::{self}` will cause any non-module items at the same path to also be imported.
/// This might cause a naming conflict (https://github.com/rust-lang/rustfmt/issues/3568). This lint makes no attempt
/// to detect this scenario and that is why it is a restriction lint.
///
/// ### Example
/// ```rust
/// use std::io::{self};
/// ```
/// Use instead:
/// ```rust
/// use std::io;
/// ```
#[clippy::version = "1.53.0"]
pub UNNECESSARY_SELF_IMPORTS,
restriction,
"imports ending in `::{self}`, which can be omitted"
}
declare_lint_pass!(UnnecessarySelfImports => [UNNECESSARY_SELF_IMPORTS]);
impl EarlyLintPass for UnnecessarySelfImports {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
if_chain! {
if let ItemKind::Use(use_tree) = &item.kind;
if let UseTreeKind::Nested(nodes) = &use_tree.kind;
if let [(self_tree, _)] = &**nodes;
if let [self_seg] = &*self_tree.prefix.segments;
if self_seg.ident.name == kw::SelfLower;
if let Some(last_segment) = use_tree.prefix.segments.last();
then {
span_lint_and_then(
cx,
UNNECESSARY_SELF_IMPORTS,
item.span,
"import ending with `::{self}`",
|diag| {
diag.span_suggestion(
last_segment.span().with_hi(item.span.hi()),
"consider omitting `::{self}`",
format!(
"{}{};",
last_segment.ident,
if let UseTreeKind::Simple(Some(alias), ..) = self_tree.kind { format!(" as {}", alias) } else { String::new() },
),
Applicability::MaybeIncorrect,
);
diag.note("this will slightly change semantics; any non-module items at the same path will also be imported");
},
);
}
}
}
}