Auto merge of #4989 - rust-lang:no-unmangled-must-use, r=flip1995

No #[no_mangle] must_use_candidate functions

This fixes #4984.

changelog: none
This commit is contained in:
bors 2020-01-03 17:04:52 +00:00
commit fddc9801dd
4 changed files with 25 additions and 9 deletions

View file

@ -1,6 +1,6 @@
use crate::utils::{ use crate::utils::{
attrs::is_proc_macro, is_must_use_ty, iter_input_pats, match_def_path, must_use_attr, qpath_res, return_ty, attr_by_name, attrs::is_proc_macro, is_must_use_ty, iter_input_pats, match_def_path, must_use_attr, qpath_res,
snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then, trait_ref_of_method, return_ty, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then, trait_ref_of_method,
type_is_unsafe_function, type_is_unsafe_function,
}; };
use matches::matches; 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); check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
return; 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( check_must_use_candidate(
cx, cx,
&sig.decl, &sig.decl,

View file

@ -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> { pub fn must_use_attr(attrs: &[Attribute]) -> Option<&Attribute> {
attrs.iter().find(|attr| { attr_by_name(attrs, "must_use")
attr.ident().map_or(false, |ident| {
let ident: &str = &ident.as_str();
"must_use" == ident
})
})
} }
// Returns whether the type has #[must_use] attribute // Returns whether the type has #[must_use] attribute

View file

@ -83,6 +83,11 @@ pub unsafe fn mutates_static() -> usize {
COUNTER COUNTER
} }
#[no_mangle]
pub fn unmangled(i: bool) -> bool {
!i
}
fn main() { fn main() {
assert_eq!(1, pure(1)); assert_eq!(1, pure(1));
} }

View file

@ -83,6 +83,11 @@ pub unsafe fn mutates_static() -> usize {
COUNTER COUNTER
} }
#[no_mangle]
pub fn unmangled(i: bool) -> bool {
!i
}
fn main() { fn main() {
assert_eq!(1, pure(1)); assert_eq!(1, pure(1));
} }