diff --git a/clippy_lints/src/functions.rs b/clippy_lints/src/functions.rs index 5b6d1d72f..81f199de5 100644 --- a/clippy_lints/src/functions.rs +++ b/clippy_lints/src/functions.rs @@ -1,6 +1,6 @@ use crate::utils::{ - attrs::is_proc_macro, is_must_use_ty, iter_input_pats, match_def_path, must_use_attr, qpath_res, return_ty, - snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then, trait_ref_of_method, + attr_by_name, attrs::is_proc_macro, is_must_use_ty, iter_input_pats, match_def_path, must_use_attr, qpath_res, + return_ty, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then, trait_ref_of_method, type_is_unsafe_function, }; use matches::matches; @@ -236,7 +236,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions { check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr); return; } - if cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) { + if cx.access_levels.is_exported(item.hir_id) + && !is_proc_macro(&item.attrs) + && attr_by_name(&item.attrs, "no_mangle").is_none() + { check_must_use_candidate( cx, &sig.decl, diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 0680f627b..98ab844ae 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -1253,13 +1253,16 @@ pub fn parent_node_is_if_expr<'a, 'b>(expr: &Expr<'_>, cx: &LateContext<'a, 'b>) } } +// Finds the attribute with the given name, if any +pub fn attr_by_name<'a>(attrs: &'a [Attribute], name: &'_ str) -> Option<&'a Attribute> { + attrs + .iter() + .find(|attr| attr.ident().map_or(false, |ident| ident.as_str() == name)) +} + +// Finds the `#[must_use]` attribute, if any pub fn must_use_attr(attrs: &[Attribute]) -> Option<&Attribute> { - attrs.iter().find(|attr| { - attr.ident().map_or(false, |ident| { - let ident: &str = &ident.as_str(); - "must_use" == ident - }) - }) + attr_by_name(attrs, "must_use") } // Returns whether the type has #[must_use] attribute diff --git a/tests/ui/must_use_candidates.fixed b/tests/ui/must_use_candidates.fixed index ff7485753..e2ceb8bad 100644 --- a/tests/ui/must_use_candidates.fixed +++ b/tests/ui/must_use_candidates.fixed @@ -83,6 +83,11 @@ pub unsafe fn mutates_static() -> usize { COUNTER } +#[no_mangle] +pub fn unmangled(i: bool) -> bool { + !i +} + fn main() { assert_eq!(1, pure(1)); } diff --git a/tests/ui/must_use_candidates.rs b/tests/ui/must_use_candidates.rs index 29c075299..29ef8d1ed 100644 --- a/tests/ui/must_use_candidates.rs +++ b/tests/ui/must_use_candidates.rs @@ -83,6 +83,11 @@ pub unsafe fn mutates_static() -> usize { COUNTER } +#[no_mangle] +pub fn unmangled(i: bool) -> bool { + !i +} + fn main() { assert_eq!(1, pure(1)); }