Skip to content

Commit 6f48e37

Browse files
authored
Merge pull request #2444 from phansch/fix_incorrect_useless_attribute_suggestion
Partly fix incorrect useless_attribute suggestion
2 parents 81b2565 + 81f5969 commit 6f48e37

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

clippy_lints/src/attrs.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::ty::{self, TyCtxt};
77
use semver::Version;
88
use syntax::ast::{Attribute, AttrStyle, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
99
use syntax::codemap::Span;
10-
use utils::{in_macro, match_def_path, opt_def_id, paths, snippet_opt, span_lint, span_lint_and_then};
10+
use utils::{in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint, span_lint_and_then};
1111

1212
/// **What it does:** Checks for items annotated with `#[inline(always)]`,
1313
/// unless the annotated function is empty or simply panics.
@@ -156,17 +156,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
156156
}
157157
}
158158
}
159-
if let Some(mut sugg) = snippet_opt(cx, attr.span) {
160-
if sugg.len() > 1 {
159+
let line_span = last_line_of_span(cx, attr.span);
160+
161+
if let Some(mut sugg) = snippet_opt(cx, line_span) {
162+
if sugg.contains("#[") {
161163
span_lint_and_then(
162164
cx,
163165
USELESS_ATTRIBUTE,
164-
attr.span,
166+
line_span,
165167
"useless lint attribute",
166168
|db| {
167-
sugg.insert(1, '!');
169+
sugg = sugg.replacen("#[", "#![", 1);
168170
db.span_suggestion(
169-
attr.span,
171+
line_span,
170172
"if you just forgot a `!`, use",
171173
sugg,
172174
);

clippy_lints/src/utils/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,14 @@ pub fn snippet_block<'a, 'b, T: LintContext<'b>>(cx: &T, span: Span, default: &'
427427
trim_multiline(snip, true)
428428
}
429429

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

tests/ui/useless_attribute.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#![warn(useless_attribute)]
44

55
#[allow(dead_code, unused_extern_crates)]
6+
#[cfg_attr(feature = "cargo-clippy", allow(dead_code, unused_extern_crates))]
7+
#[cfg_attr(feature = "cargo-clippy",
8+
allow(dead_code, unused_extern_crates))]
69
extern crate clippy_lints;
710

811
// don't lint on unused_import for `use` items

tests/ui/useless_attribute.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@ error: useless lint attribute
66
|
77
= note: `-D useless-attribute` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: useless lint attribute
10+
--> $DIR/useless_attribute.rs:6:1
11+
|
12+
6 | #[cfg_attr(feature = "cargo-clippy", allow(dead_code, unused_extern_crates))]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![cfg_attr(feature = "cargo-clippy", allow(dead_code, unused_extern_crates))`
14+
15+
error: aborting due to 2 previous errors
1016

0 commit comments

Comments
 (0)