Skip to content

Commit

Permalink
Merge pull request #2444 from phansch/fix_incorrect_useless_attribute…
Browse files Browse the repository at this point in the history
…_suggestion

Partly fix incorrect useless_attribute suggestion
  • Loading branch information
oli-obk authored Feb 7, 2018
2 parents 81b2565 + 81f5969 commit 6f48e37
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
14 changes: 8 additions & 6 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
);
Expand Down
8 changes: 8 additions & 0 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>(
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/useless_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/useless_attribute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6f48e37

Please sign in to comment.