Skip to content

No #[no_mangle] must_use_candidate functions #4989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions clippy_lints/src/functions.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/must_use_candidates.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
5 changes: 5 additions & 0 deletions tests/ui/must_use_candidates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}