Skip to content

Commit 850ad52

Browse files
committed
Auto merge of #4696 - rust-lang:need-not-use-proc-macro, r=phansch
Omit proc macros from `must_use_candidate` This fixes #4684. changelog: none
2 parents c0b2411 + d723b35 commit 850ad52

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

clippy_lints/src/functions.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{
2-
iter_input_pats, match_def_path, qpath_res, return_ty, snippet, snippet_opt, span_help_and_lint, span_lint,
3-
span_lint_and_then, type_is_unsafe_function,
2+
attrs::is_proc_macro, iter_input_pats, match_def_path, qpath_res, return_ty, snippet, snippet_opt,
3+
span_help_and_lint, span_lint, span_lint_and_then, type_is_unsafe_function,
44
};
55
use matches::matches;
66
use rustc::hir::{self, def::Res, def_id::DefId, intravisit};
@@ -234,7 +234,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
234234
check_needless_must_use(cx, decl, item.hir_id, item.span, fn_header_span, attr);
235235
return;
236236
}
237-
if cx.access_levels.is_exported(item.hir_id) {
237+
if cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) {
238238
check_must_use_candidate(
239239
cx,
240240
decl,
@@ -254,7 +254,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
254254
if let Some(attr) = attr {
255255
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
256256
check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
257-
} else if cx.access_levels.is_exported(item.hir_id) {
257+
} else if cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) {
258258
check_must_use_candidate(
259259
cx,
260260
&sig.decl,
@@ -284,7 +284,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
284284
let body = cx.tcx.hir().body(eid);
285285
Self::check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.hir_id);
286286

287-
if attr.is_none() && cx.access_levels.is_exported(item.hir_id) {
287+
if attr.is_none() && cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) {
288288
check_must_use_candidate(
289289
cx,
290290
&sig.decl,

clippy_lints/src/utils/attrs.rs

+13
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,16 @@ fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'
114114
}
115115
}
116116
}
117+
118+
/// Return true if the attributes contain any of `proc_macro`,
119+
/// `proc_macro_derive` or `proc_macro_attribute`, false otherwise
120+
pub fn is_proc_macro(attrs: &[ast::Attribute]) -> bool {
121+
use syntax_pos::Symbol;
122+
123+
let syms = [
124+
Symbol::intern("proc_macro"),
125+
Symbol::intern("proc_macro_derive"),
126+
Symbol::intern("proc_macro_attribute"),
127+
];
128+
attrs.iter().any(|attr| syms.iter().any(move |&s| attr.check_name(s)))
129+
}

tests/ui/proc_macro.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
//! Check that we correctly lint procedural macros.
2-
32
#![crate_type = "proc-macro"]
43

4+
extern crate proc_macro;
5+
6+
use proc_macro::TokenStream;
7+
58
#[allow(dead_code)]
69
fn f() {
710
let _x = 3.14;
811
}
12+
13+
#[proc_macro]
14+
pub fn mybangmacro(t: TokenStream) -> TokenStream {
15+
t
16+
}
17+
18+
#[proc_macro_derive(MyDerivedTrait)]
19+
pub fn myderive(t: TokenStream) -> TokenStream {
20+
t
21+
}
22+
23+
#[proc_macro_attribute]
24+
pub fn myattribute(t: TokenStream, a: TokenStream) -> TokenStream {
25+
t
26+
}

tests/ui/proc_macro.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: approximate value of `f{32, 64}::consts::PI` found. Consider using it directly
2-
--> $DIR/proc_macro.rs:7:14
2+
--> $DIR/proc_macro.rs:10:14
33
|
44
LL | let _x = 3.14;
55
| ^^^^

0 commit comments

Comments
 (0)