Skip to content

Commit 0da0ae3

Browse files
committed
Auto merge of rust-lang#5022 - flip1995:useless_attr, r=phansch
Fix useless_attribute suggestion Fixes rust-lang#5021 changelog: Fix [`useless_attribute`] suggestion, which tripped rustfix
2 parents 52b9e70 + 6eba665 commit 0da0ae3

File tree

4 files changed

+88
-6
lines changed

4 files changed

+88
-6
lines changed

clippy_lints/src/utils/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
4242
use rustc_hir::Node;
4343
use rustc_hir::*;
4444
use rustc_span::hygiene::ExpnKind;
45-
use rustc_span::source_map::{Span, DUMMY_SP};
4645
use rustc_span::symbol::{kw, Symbol};
46+
use rustc_span::{BytePos, Pos, Span, DUMMY_SP};
4747
use smallvec::SmallVec;
4848
use syntax::ast::{self, Attribute, LitKind};
4949
use syntax::attr;
@@ -554,7 +554,16 @@ pub fn last_line_of_span<T: LintContext>(cx: &T, span: Span) -> Span {
554554
let source_map_and_line = cx.sess().source_map().lookup_line(span.lo()).unwrap();
555555
let line_no = source_map_and_line.line;
556556
let line_start = &source_map_and_line.sf.lines[line_no];
557-
Span::new(*line_start, span.hi(), span.ctxt())
557+
let span = Span::new(*line_start, span.hi(), span.ctxt());
558+
if_chain! {
559+
if let Some(snip) = snippet_opt(cx, span);
560+
if let Some(first_ch_pos) = snip.find(|c: char| !c.is_whitespace());
561+
then {
562+
span.with_lo(span.lo() + BytePos::from_usize(first_ch_pos))
563+
} else {
564+
span
565+
}
566+
}
558567
}
559568

560569
/// Like `snippet_block`, but add braces if the expr is not an `ExprKind::Block`.

tests/ui/useless_attribute.fixed

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// run-rustfix
2+
// aux-build:proc_macro_derive.rs
3+
4+
#![warn(clippy::useless_attribute)]
5+
#![warn(unreachable_pub)]
6+
#![feature(rustc_private)]
7+
8+
#![allow(dead_code)]
9+
#![cfg_attr(feature = "cargo-clippy", allow(dead_code))]
10+
#[rustfmt::skip]
11+
#[allow(unused_imports)]
12+
#[allow(unused_extern_crates)]
13+
#[macro_use]
14+
extern crate rustc;
15+
16+
#[macro_use]
17+
extern crate proc_macro_derive;
18+
19+
// don't lint on unused_import for `use` items
20+
#[allow(unused_imports)]
21+
use std::collections;
22+
23+
// don't lint on deprecated for `use` items
24+
mod foo {
25+
#[deprecated]
26+
pub struct Bar;
27+
}
28+
#[allow(deprecated)]
29+
pub use foo::Bar;
30+
31+
// This should not trigger the lint. There's lint level definitions inside the external derive
32+
// that would trigger the useless_attribute lint.
33+
#[derive(DeriveSomething)]
34+
struct Baz;
35+
36+
// don't lint on unreachable_pub for `use` items
37+
mod a {
38+
mod b {
39+
#[allow(dead_code)]
40+
#[allow(unreachable_pub)]
41+
pub struct C {}
42+
}
43+
44+
#[allow(unreachable_pub)]
45+
pub use self::b::C;
46+
}
47+
48+
fn test_indented_attr() {
49+
#![allow(clippy::almost_swapped)]
50+
use std::collections::HashSet;
51+
52+
let _ = HashSet::<u32>::default();
53+
}
54+
55+
fn main() {
56+
test_indented_attr();
57+
}

tests/ui/useless_attribute.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
// aux-build:proc_macro_derive.rs
23

34
#![warn(clippy::useless_attribute)]
@@ -44,4 +45,13 @@ mod a {
4445
pub use self::b::C;
4546
}
4647

47-
fn main() {}
48+
fn test_indented_attr() {
49+
#[allow(clippy::almost_swapped)]
50+
use std::collections::HashSet;
51+
52+
let _ = HashSet::<u32>::default();
53+
}
54+
55+
fn main() {
56+
test_indented_attr();
57+
}

tests/ui/useless_attribute.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
error: useless lint attribute
2-
--> $DIR/useless_attribute.rs:7:1
2+
--> $DIR/useless_attribute.rs:8:1
33
|
44
LL | #[allow(dead_code)]
55
| ^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(dead_code)]`
66
|
77
= note: `-D clippy::useless-attribute` implied by `-D warnings`
88

99
error: useless lint attribute
10-
--> $DIR/useless_attribute.rs:8:1
10+
--> $DIR/useless_attribute.rs:9:1
1111
|
1212
LL | #[cfg_attr(feature = "cargo-clippy", allow(dead_code))]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![cfg_attr(feature = "cargo-clippy", allow(dead_code)`
1414

15-
error: aborting due to 2 previous errors
15+
error: useless lint attribute
16+
--> $DIR/useless_attribute.rs:49:5
17+
|
18+
LL | #[allow(clippy::almost_swapped)]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(clippy::almost_swapped)]`
20+
21+
error: aborting due to 3 previous errors
1622

0 commit comments

Comments
 (0)