Skip to content

Commit 79d870e

Browse files
authored
Merge pull request #2777 from rust-lang-nursery/scoped_attrs
Use the new scoped tool attributes
2 parents fcbcdbc + e0df4cc commit 79d870e

14 files changed

+59
-64
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,8 @@ First, create a new UI test file in the `tests/ui/` directory with the pattern y
8080

8181
```rust
8282
// ./tests/ui/my_lint.rs
83-
84-
// The custom_attribute needs to be enabled for the author lint to work
85-
#![feature(plugin, custom_attribute)]
86-
8783
fn main() {
88-
#[clippy(author)]
84+
#[clippy::author]
8985
let arr: [i32; 1] = [7]; // Replace line with the code you want to match
9086
}
9187
```

clippy_lints/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// error-pattern:cargo-clippy
22

33
#![feature(box_syntax)]
4-
#![feature(custom_attribute)]
54
#![feature(rustc_private)]
65
#![feature(slice_patterns)]
76
#![feature(stmt_expr_attributes)]

clippy_lints/src/utils/author.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use rustc::lint::*;
77
use rustc::hir;
88
use rustc::hir::{Expr, Expr_, QPath, Ty_, Pat, PatKind, BindingAnnotation, StmtSemi, StmtExpr, StmtDecl, Decl_, Stmt};
99
use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
10-
use syntax::ast::{self, Attribute, LitKind, DUMMY_NODE_ID};
10+
use syntax::ast::{Attribute, LitKind, DUMMY_NODE_ID};
1111
use std::collections::HashMap;
12+
use utils::get_attr;
1213

1314
/// **What it does:** Generates clippy code that detects the offending pattern
1415
///
@@ -17,10 +18,10 @@ use std::collections::HashMap;
1718
/// // ./tests/ui/my_lint.rs
1819
/// fn foo() {
1920
/// // detect the following pattern
20-
/// #[clippy(author)]
21+
/// #[clippy::author]
2122
/// if x == 42 {
2223
/// // but ignore everything from here on
23-
/// #![clippy(author = "ignore")]
24+
/// #![clippy::author = "ignore"]
2425
/// }
2526
/// }
2627
/// ```
@@ -633,14 +634,7 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
633634
}
634635

635636
fn has_attr(attrs: &[Attribute]) -> bool {
636-
attrs.iter().any(|attr| {
637-
attr.check_name("clippy") && attr.meta_item_list().map_or(false, |list| {
638-
list.len() == 1 && match list[0].node {
639-
ast::NestedMetaItemKind::MetaItem(ref it) => it.name() == "author",
640-
ast::NestedMetaItemKind::Literal(_) => false,
641-
}
642-
})
643-
})
637+
get_attr(attrs, "author").count() > 0
644638
}
645639

646640
fn desugaring_name(des: hir::MatchSource) -> String {

clippy_lints/src/utils/inspector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc::lint::*;
66
use rustc::hir;
77
use rustc::hir::print;
88
use syntax::ast::Attribute;
9-
use syntax::attr;
9+
use utils::get_attr;
1010

1111
/// **What it does:** Dumps every ast/hir node which has the `#[clippy_dump]`
1212
/// attribute
@@ -136,7 +136,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
136136
}
137137

138138
fn has_attr(attrs: &[Attribute]) -> bool {
139-
attr::contains_name(attrs, "clippy_dump")
139+
get_attr(attrs, "dump").count() > 0
140140
}
141141

142142
fn print_decl(cx: &LateContext, decl: &hir::Decl) {

clippy_lints/src/utils/mod.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -735,20 +735,26 @@ impl LimitStack {
735735
}
736736
}
737737

738-
fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'static str, mut f: F) {
739-
for attr in attrs {
740-
if attr.is_sugared_doc {
741-
continue;
738+
pub fn get_attr<'a>(attrs: &'a [ast::Attribute], name: &'static str) -> impl Iterator<Item = &'a ast::Attribute> {
739+
attrs.iter().filter_map(move |attr| {
740+
if attr.path.segments.len() == 2 && attr.path.segments[0].ident.to_string() == "clippy" && attr.path.segments[1].ident.to_string() == name {
741+
Some(attr)
742+
} else {
743+
None
742744
}
745+
})
746+
}
747+
748+
fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'static str, mut f: F) {
749+
for attr in get_attr(attrs, name) {
743750
if let Some(ref value) = attr.value_str() {
744-
if attr.name() == name {
745-
if let Ok(value) = FromStr::from_str(&value.as_str()) {
746-
attr::mark_used(attr);
747-
f(value)
748-
} else {
749-
sess.span_err(attr.span, "not a number");
750-
}
751+
if let Ok(value) = FromStr::from_str(&value.as_str()) {
752+
f(value)
753+
} else {
754+
sess.span_err(attr.span, "not a number");
751755
}
756+
} else {
757+
sess.span_err(attr.span, "bad clippy attribute");
752758
}
753759
}
754760
}

tests/ui/author.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#![feature(plugin, custom_attribute)]
1+
#![feature(tool_attributes)]
22

33
fn main() {
44

5-
#[clippy(author)]
5+
#[clippy::author]
66
let x: char = 0x45 as char;
77
}

tests/ui/author/for_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#![feature(custom_attribute)]
1+
#![feature(tool_attributes)]
22

33
fn main() {
4-
#[clippy(author)]
4+
#[clippy::author]
55
for y in 0..10 {
66
let z = y;
77
}

tests/ui/author/matches.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#![feature(custom_attribute)]
1+
#![feature(tool_attributes)]
22

33
fn main() {
4-
#[clippy(author)]
4+
#[clippy::author]
55
let a = match 42 {
66
16 => 5,
77
17 => {

tests/ui/cyclomatic_complexity.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(custom_attribute)]
1+
#![feature(tool_attributes)]
22

33
#![allow(clippy)]
44
#![warn(cyclomatic_complexity)]
@@ -88,7 +88,7 @@ fn main() {
8888
}
8989
}
9090

91-
#[cyclomatic_complexity = "0"]
91+
#[clippy::cyclomatic_complexity = "0"]
9292
fn kaboom() {
9393
let n = 0;
9494
'a: for i in 0..20 {
@@ -134,17 +134,17 @@ fn bloo() {
134134
}
135135
}
136136

137-
#[cyclomatic_complexity = "0"]
137+
#[clippy::cyclomatic_complexity = "0"]
138138
fn lots_of_short_circuits() -> bool {
139139
true && false && true && false && true && false && true
140140
}
141141

142-
#[cyclomatic_complexity = "0"]
142+
#[clippy::cyclomatic_complexity = "0"]
143143
fn lots_of_short_circuits2() -> bool {
144144
true || false || true || false || true || false || true
145145
}
146146

147-
#[cyclomatic_complexity = "0"]
147+
#[clippy::cyclomatic_complexity = "0"]
148148
fn baa() {
149149
let x = || match 99 {
150150
0 => 0,
@@ -162,7 +162,7 @@ fn baa() {
162162
}
163163
}
164164

165-
#[cyclomatic_complexity = "0"]
165+
#[clippy::cyclomatic_complexity = "0"]
166166
fn bar() {
167167
match 99 {
168168
0 => println!("hi"),
@@ -171,7 +171,7 @@ fn bar() {
171171
}
172172

173173
#[test]
174-
#[cyclomatic_complexity = "0"]
174+
#[clippy::cyclomatic_complexity = "0"]
175175
/// Tests are usually complex but simple at the same time. `cyclomatic_complexity` used to give
176176
/// lots of false-positives in tests.
177177
fn dont_warn_on_tests() {
@@ -181,7 +181,7 @@ fn dont_warn_on_tests() {
181181
}
182182
}
183183

184-
#[cyclomatic_complexity = "0"]
184+
#[clippy::cyclomatic_complexity = "0"]
185185
fn barr() {
186186
match 99 {
187187
0 => println!("hi"),
@@ -191,7 +191,7 @@ fn barr() {
191191
}
192192
}
193193

194-
#[cyclomatic_complexity = "0"]
194+
#[clippy::cyclomatic_complexity = "0"]
195195
fn barr2() {
196196
match 99 {
197197
0 => println!("hi"),
@@ -207,7 +207,7 @@ fn barr2() {
207207
}
208208
}
209209

210-
#[cyclomatic_complexity = "0"]
210+
#[clippy::cyclomatic_complexity = "0"]
211211
fn barrr() {
212212
match 99 {
213213
0 => println!("hi"),
@@ -217,7 +217,7 @@ fn barrr() {
217217
}
218218
}
219219

220-
#[cyclomatic_complexity = "0"]
220+
#[clippy::cyclomatic_complexity = "0"]
221221
fn barrr2() {
222222
match 99 {
223223
0 => println!("hi"),
@@ -233,7 +233,7 @@ fn barrr2() {
233233
}
234234
}
235235

236-
#[cyclomatic_complexity = "0"]
236+
#[clippy::cyclomatic_complexity = "0"]
237237
fn barrrr() {
238238
match 99 {
239239
0 => println!("hi"),
@@ -243,7 +243,7 @@ fn barrrr() {
243243
}
244244
}
245245

246-
#[cyclomatic_complexity = "0"]
246+
#[clippy::cyclomatic_complexity = "0"]
247247
fn barrrr2() {
248248
match 99 {
249249
0 => println!("hi"),
@@ -259,7 +259,7 @@ fn barrrr2() {
259259
}
260260
}
261261

262-
#[cyclomatic_complexity = "0"]
262+
#[clippy::cyclomatic_complexity = "0"]
263263
fn cake() {
264264
if 4 == 5 {
265265
println!("yea");
@@ -270,7 +270,7 @@ fn cake() {
270270
}
271271

272272

273-
#[cyclomatic_complexity = "0"]
273+
#[clippy::cyclomatic_complexity = "0"]
274274
pub fn read_file(input_path: &str) -> String {
275275
use std::fs::File;
276276
use std::io::{Read, Write};
@@ -301,29 +301,29 @@ pub fn read_file(input_path: &str) -> String {
301301

302302
enum Void {}
303303

304-
#[cyclomatic_complexity = "0"]
304+
#[clippy::cyclomatic_complexity = "0"]
305305
fn void(void: Void) {
306306
if true {
307307
match void {
308308
}
309309
}
310310
}
311311

312-
#[cyclomatic_complexity = "0"]
312+
#[clippy::cyclomatic_complexity = "0"]
313313
fn mcarton_sees_all() {
314314
panic!("meh");
315315
panic!("möh");
316316
}
317317

318-
#[cyclomatic_complexity = "0"]
318+
#[clippy::cyclomatic_complexity = "0"]
319319
fn try() -> Result<i32, &'static str> {
320320
match 5 {
321321
5 => Ok(5),
322322
_ => return Err("bla"),
323323
}
324324
}
325325

326-
#[cyclomatic_complexity = "0"]
326+
#[clippy::cyclomatic_complexity = "0"]
327327
fn try_again() -> Result<i32, &'static str> {
328328
let _ = try!(Ok(42));
329329
let _ = try!(Ok(43));
@@ -339,7 +339,7 @@ fn try_again() -> Result<i32, &'static str> {
339339
}
340340
}
341341

342-
#[cyclomatic_complexity = "0"]
342+
#[clippy::cyclomatic_complexity = "0"]
343343
fn early() -> Result<i32, &'static str> {
344344
return Ok(5);
345345
return Ok(5);
@@ -352,7 +352,7 @@ fn early() -> Result<i32, &'static str> {
352352
return Ok(5);
353353
}
354354

355-
#[cyclomatic_complexity = "0"]
355+
#[clippy::cyclomatic_complexity = "0"]
356356
fn early_ret() -> i32 {
357357
let a = if true { 42 } else { return 0; };
358358
let a = if a < 99 { 42 } else { return 0; };

tests/ui/cyclomatic_complexity_attr_used.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(custom_attribute)]
1+
#![feature(tool_attributes)]
22

33
#![warn(cyclomatic_complexity)]
44
#![warn(unused)]
@@ -7,7 +7,7 @@ fn main() {
77
kaboom();
88
}
99

10-
#[cyclomatic_complexity = "0"]
10+
#[clippy::cyclomatic_complexity = "0"]
1111
fn kaboom() {
1212
if 42 == 43 {
1313
panic!();

tests/ui/excessive_precision.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(custom_attribute)]
1+
22
#![warn(excessive_precision)]
33
#![allow(print_literal)]
44

tests/ui/for_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(custom_attribute)]
1+
22

33

44
use std::collections::*;

tests/ui/trailing_zeros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#![feature(custom_attribute, stmt_expr_attributes)]
1+
#![feature(stmt_expr_attributes, tool_attributes)]
22

33
#![allow(unused_parens)]
44

55
fn main() {
66
let x: i32 = 42;
7-
let _ = #[clippy(author)] (x & 0b1111 == 0); // suggest trailing_zeros
7+
let _ = #[clippy::author] (x & 0b1111 == 0); // suggest trailing_zeros
88
let _ = x & 0b1_1111 == 0; // suggest trailing_zeros
99
let _ = x & 0b1_1010 == 0; // do not lint
1010
let _ = x & 1 == 0; // do not lint

tests/ui/trailing_zeros.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: bit mask could be simplified with a call to `trailing_zeros`
22
--> $DIR/trailing_zeros.rs:7:31
33
|
4-
7 | let _ = #[clippy(author)] (x & 0b1111 == 0); // suggest trailing_zeros
4+
7 | let _ = #[clippy::author] (x & 0b1111 == 0); // suggest trailing_zeros
55
| ^^^^^^^^^^^^^^^^^ help: try: `x.trailing_zeros() >= 4`
66
|
77
= note: `-D verbose-bit-mask` implied by `-D warnings`

0 commit comments

Comments
 (0)