Skip to content

Commit

Permalink
Omit proc macros from must_use_candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
llogiq committed Oct 18, 2019
1 parent c0b2411 commit d723b35
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
10 changes: 5 additions & 5 deletions clippy_lints/src/functions.rs
@@ -1,6 +1,6 @@
use crate::utils::{
iter_input_pats, match_def_path, qpath_res, return_ty, snippet, snippet_opt, span_help_and_lint, span_lint,
span_lint_and_then, type_is_unsafe_function,
attrs::is_proc_macro, iter_input_pats, match_def_path, qpath_res, return_ty, snippet, snippet_opt,
span_help_and_lint, span_lint, span_lint_and_then, type_is_unsafe_function,
};
use matches::matches;
use rustc::hir::{self, def::Res, def_id::DefId, intravisit};
Expand Down Expand Up @@ -234,7 +234,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
check_needless_must_use(cx, decl, item.hir_id, item.span, fn_header_span, attr);
return;
}
if cx.access_levels.is_exported(item.hir_id) {
if cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) {
check_must_use_candidate(
cx,
decl,
Expand All @@ -254,7 +254,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
if let Some(attr) = attr {
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
} else if cx.access_levels.is_exported(item.hir_id) {
} else if cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) {
check_must_use_candidate(
cx,
&sig.decl,
Expand Down Expand Up @@ -284,7 +284,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
let body = cx.tcx.hir().body(eid);
Self::check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.hir_id);

if attr.is_none() && cx.access_levels.is_exported(item.hir_id) {
if attr.is_none() && cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) {
check_must_use_candidate(
cx,
&sig.decl,
Expand Down
13 changes: 13 additions & 0 deletions clippy_lints/src/utils/attrs.rs
Expand Up @@ -114,3 +114,16 @@ fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'
}
}
}

/// Return true if the attributes contain any of `proc_macro`,
/// `proc_macro_derive` or `proc_macro_attribute`, false otherwise
pub fn is_proc_macro(attrs: &[ast::Attribute]) -> bool {
use syntax_pos::Symbol;

let syms = [
Symbol::intern("proc_macro"),
Symbol::intern("proc_macro_derive"),
Symbol::intern("proc_macro_attribute"),
];
attrs.iter().any(|attr| syms.iter().any(move |&s| attr.check_name(s)))
}
20 changes: 19 additions & 1 deletion tests/ui/proc_macro.rs
@@ -1,8 +1,26 @@
//! Check that we correctly lint procedural macros.

#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::TokenStream;

#[allow(dead_code)]
fn f() {
let _x = 3.14;
}

#[proc_macro]
pub fn mybangmacro(t: TokenStream) -> TokenStream {
t
}

#[proc_macro_derive(MyDerivedTrait)]
pub fn myderive(t: TokenStream) -> TokenStream {
t
}

#[proc_macro_attribute]
pub fn myattribute(t: TokenStream, a: TokenStream) -> TokenStream {
t
}
2 changes: 1 addition & 1 deletion tests/ui/proc_macro.stderr
@@ -1,5 +1,5 @@
error: approximate value of `f{32, 64}::consts::PI` found. Consider using it directly
--> $DIR/proc_macro.rs:7:14
--> $DIR/proc_macro.rs:10:14
|
LL | let _x = 3.14;
| ^^^^
Expand Down

0 comments on commit d723b35

Please sign in to comment.