Skip to content

Commit fddc980

Browse files
committed
Auto merge of rust-lang#4989 - rust-lang:no-unmangled-must-use, r=flip1995
No #[no_mangle] must_use_candidate functions This fixes rust-lang#4984. changelog: none
2 parents 2e8c3c3 + 47972cd commit fddc980

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

clippy_lints/src/functions.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{
2-
attrs::is_proc_macro, is_must_use_ty, iter_input_pats, match_def_path, must_use_attr, qpath_res, return_ty,
3-
snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then, trait_ref_of_method,
2+
attr_by_name, attrs::is_proc_macro, is_must_use_ty, iter_input_pats, match_def_path, must_use_attr, qpath_res,
3+
return_ty, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then, trait_ref_of_method,
44
type_is_unsafe_function,
55
};
66
use matches::matches;
@@ -236,7 +236,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
236236
check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
237237
return;
238238
}
239-
if cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) {
239+
if cx.access_levels.is_exported(item.hir_id)
240+
&& !is_proc_macro(&item.attrs)
241+
&& attr_by_name(&item.attrs, "no_mangle").is_none()
242+
{
240243
check_must_use_candidate(
241244
cx,
242245
&sig.decl,

clippy_lints/src/utils/mod.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1253,13 +1253,16 @@ pub fn parent_node_is_if_expr<'a, 'b>(expr: &Expr<'_>, cx: &LateContext<'a, 'b>)
12531253
}
12541254
}
12551255

1256+
// Finds the attribute with the given name, if any
1257+
pub fn attr_by_name<'a>(attrs: &'a [Attribute], name: &'_ str) -> Option<&'a Attribute> {
1258+
attrs
1259+
.iter()
1260+
.find(|attr| attr.ident().map_or(false, |ident| ident.as_str() == name))
1261+
}
1262+
1263+
// Finds the `#[must_use]` attribute, if any
12561264
pub fn must_use_attr(attrs: &[Attribute]) -> Option<&Attribute> {
1257-
attrs.iter().find(|attr| {
1258-
attr.ident().map_or(false, |ident| {
1259-
let ident: &str = &ident.as_str();
1260-
"must_use" == ident
1261-
})
1262-
})
1265+
attr_by_name(attrs, "must_use")
12631266
}
12641267

12651268
// Returns whether the type has #[must_use] attribute

tests/ui/must_use_candidates.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ pub unsafe fn mutates_static() -> usize {
8383
COUNTER
8484
}
8585

86+
#[no_mangle]
87+
pub fn unmangled(i: bool) -> bool {
88+
!i
89+
}
90+
8691
fn main() {
8792
assert_eq!(1, pure(1));
8893
}

tests/ui/must_use_candidates.rs

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ pub unsafe fn mutates_static() -> usize {
8383
COUNTER
8484
}
8585

86+
#[no_mangle]
87+
pub fn unmangled(i: bool) -> bool {
88+
!i
89+
}
90+
8691
fn main() {
8792
assert_eq!(1, pure(1));
8893
}

0 commit comments

Comments
 (0)