Merge pull request #2444 from phansch/fix_incorrect_useless_attribute_suggestion

Partly fix incorrect useless_attribute suggestion
This commit is contained in:
Oliver Schneider 2018-02-07 09:07:46 +01:00 committed by GitHub
commit 6f48e37d22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 7 deletions

View file

@ -7,7 +7,7 @@ use rustc::ty::{self, TyCtxt};
use semver::Version;
use syntax::ast::{Attribute, AttrStyle, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
use syntax::codemap::Span;
use utils::{in_macro, match_def_path, opt_def_id, paths, snippet_opt, span_lint, span_lint_and_then};
use utils::{in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint, span_lint_and_then};
/// **What it does:** Checks for items annotated with `#[inline(always)]`,
/// unless the annotated function is empty or simply panics.
@ -156,17 +156,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
}
}
}
if let Some(mut sugg) = snippet_opt(cx, attr.span) {
if sugg.len() > 1 {
let line_span = last_line_of_span(cx, attr.span);
if let Some(mut sugg) = snippet_opt(cx, line_span) {
if sugg.contains("#[") {
span_lint_and_then(
cx,
USELESS_ATTRIBUTE,
attr.span,
line_span,
"useless lint attribute",
|db| {
sugg.insert(1, '!');
sugg = sugg.replacen("#[", "#![", 1);
db.span_suggestion(
attr.span,
line_span,
"if you just forgot a `!`, use",
sugg,
);

View file

@ -427,6 +427,14 @@ pub fn snippet_block<'a, 'b, T: LintContext<'b>>(cx: &T, span: Span, default: &'
trim_multiline(snip, true)
}
/// Returns a new Span that covers the full last line of the given Span
pub fn last_line_of_span<'a, T: LintContext<'a>>(cx: &T, span: Span) -> Span {
let file_map_and_line = cx.sess().codemap().lookup_line(span.lo()).unwrap();
let line_no = file_map_and_line.line;
let line_start = &file_map_and_line.fm.lines.clone().into_inner()[line_no];
Span::new(*line_start, span.hi(), span.ctxt())
}
/// Like `snippet_block`, but add braces if the expr is not an `ExprBlock`.
/// Also takes an `Option<String>` which can be put inside the braces.
pub fn expr_block<'a, 'b, T: LintContext<'b>>(

View file

@ -3,6 +3,9 @@
#![warn(useless_attribute)]
#[allow(dead_code, unused_extern_crates)]
#[cfg_attr(feature = "cargo-clippy", allow(dead_code, unused_extern_crates))]
#[cfg_attr(feature = "cargo-clippy",
allow(dead_code, unused_extern_crates))]
extern crate clippy_lints;
// don't lint on unused_import for `use` items

View file

@ -6,5 +6,11 @@ error: useless lint attribute
|
= note: `-D useless-attribute` implied by `-D warnings`
error: aborting due to previous error
error: useless lint attribute
--> $DIR/useless_attribute.rs:6:1
|
6 | #[cfg_attr(feature = "cargo-clippy", allow(dead_code, unused_extern_crates))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![cfg_attr(feature = "cargo-clippy", allow(dead_code, unused_extern_crates))`
error: aborting due to 2 previous errors