Skip to content

Commit eb9f9b1

Browse files
committed
Auto merge of #3932 - phansch:2910, r=flip1995
Don't emit useless_attribute lint in external macros Fixes #2910
2 parents 37f5c1e + 2b0dc39 commit eb9f9b1

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

clippy_lints/src/attrs.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crate::utils::{
88
use if_chain::if_chain;
99
use rustc::hir::*;
1010
use rustc::lint::{
11-
CheckLintNameResult, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass,
11+
in_external_macro, CheckLintNameResult, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray,
12+
LintContext, LintPass,
1213
};
1314
use rustc::ty::{self, TyCtxt};
1415
use rustc::{declare_tool_lint, lint_array};
@@ -241,6 +242,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
241242
let skip_unused_imports = item.attrs.iter().any(|attr| attr.check_name("macro_use"));
242243

243244
for attr in &item.attrs {
245+
if in_external_macro(cx.sess(), attr.span) {
246+
return;
247+
}
244248
if let Some(lint_list) = &attr.meta_item_list() {
245249
if let Some(ident) = attr.ident() {
246250
match &*ident.as_str() {
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// no-prefer-dynamic
2+
3+
#![crate_type = "proc-macro"]
4+
#![feature(repr128, proc_macro_hygiene, proc_macro_quote)]
5+
6+
extern crate proc_macro;
7+
8+
use proc_macro::{quote, TokenStream};
9+
10+
#[proc_macro_derive(DeriveSomething)]
11+
pub fn derive(_: TokenStream) -> TokenStream {
12+
let output = quote! {
13+
#[allow(dead_code)]
14+
extern crate clippy_lints;
15+
};
16+
output
17+
}

tests/ui/useless_attribute.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// aux-build:proc_macro_derive.rs
2+
13
#![warn(clippy::useless_attribute)]
24

35
#[allow(dead_code)]
@@ -10,6 +12,9 @@
1012
#[macro_use]
1113
extern crate clippy_lints;
1214

15+
#[macro_use]
16+
extern crate proc_macro_derive;
17+
1318
// don't lint on unused_import for `use` items
1419
#[allow(unused_imports)]
1520
use std::collections;
@@ -22,4 +27,9 @@ mod foo {
2227
#[allow(deprecated)]
2328
pub use foo::Bar;
2429

30+
// This should not trigger the lint. There's lint level definitions inside the external derive
31+
// that would trigger the useless_attribute lint.
32+
#[derive(DeriveSomething)]
33+
struct Baz;
34+
2535
fn main() {}

tests/ui/useless_attribute.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: useless lint attribute
2-
--> $DIR/useless_attribute.rs:3:1
2+
--> $DIR/useless_attribute.rs:5: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:4:1
10+
--> $DIR/useless_attribute.rs:6: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)`

0 commit comments

Comments
 (0)