Skip to content

Commit d602743

Browse files
committed
only focus on double_must_use + Add Result<(), ()> test
1 parent e2742a0 commit d602743

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

clippy_lints/src/functions/must_use.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use hir::FnSig;
12
use rustc_ast::ast::Attribute;
23
use rustc_errors::Applicability;
34
use rustc_hir::def_id::DefIdSet;
@@ -23,12 +24,11 @@ use super::{DOUBLE_MUST_USE, MUST_USE_CANDIDATE, MUST_USE_UNIT};
2324
pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
2425
let attrs = cx.tcx.hir().attrs(item.hir_id());
2526
let attr = cx.tcx.get_attr(item.owner_id, sym::must_use);
26-
if let hir::ItemKind::Fn(ref sig, _generics, ref body_id) = item.kind && !sig.header.is_async() /* (#10486) */ {
27-
27+
if let hir::ItemKind::Fn(ref sig, _generics, ref body_id) = item.kind {
2828
let is_public = cx.effective_visibilities.is_exported(item.owner_id.def_id);
2929
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
3030
if let Some(attr) = attr {
31-
check_needless_must_use(cx, sig.decl, item.owner_id, item.span, fn_header_span, attr);
31+
check_needless_must_use(cx, sig.decl, item.owner_id, item.span, fn_header_span, attr, sig);
3232
} else if is_public && !is_proc_macro(attrs) && !attrs.iter().any(|a| a.has_name(sym::no_mangle)) {
3333
check_must_use_candidate(
3434
cx,
@@ -44,13 +44,13 @@ pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>
4444
}
4545

4646
pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
47-
if let hir::ImplItemKind::Fn(ref sig, ref body_id) = item.kind && !sig.header.is_async() /* (#10486) */ {
47+
if let hir::ImplItemKind::Fn(ref sig, ref body_id) = item.kind {
4848
let is_public = cx.effective_visibilities.is_exported(item.owner_id.def_id);
4949
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
5050
let attrs = cx.tcx.hir().attrs(item.hir_id());
5151
let attr = cx.tcx.get_attr(item.owner_id, sym::must_use);
5252
if let Some(attr) = attr {
53-
check_needless_must_use(cx, sig.decl, item.owner_id, item.span, fn_header_span, attr);
53+
check_needless_must_use(cx, sig.decl, item.owner_id, item.span, fn_header_span, attr, sig);
5454
} else if is_public && !is_proc_macro(attrs) && trait_ref_of_method(cx, item.owner_id.def_id).is_none() {
5555
check_must_use_candidate(
5656
cx,
@@ -66,14 +66,14 @@ pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Imp
6666
}
6767

6868
pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
69-
if let hir::TraitItemKind::Fn(ref sig, ref eid) = item.kind && !sig.header.is_async() /* (#10486) */ {
69+
if let hir::TraitItemKind::Fn(ref sig, ref eid) = item.kind {
7070
let is_public = cx.effective_visibilities.is_exported(item.owner_id.def_id);
7171
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
7272

7373
let attrs = cx.tcx.hir().attrs(item.hir_id());
7474
let attr = cx.tcx.get_attr(item.owner_id, sym::must_use);
7575
if let Some(attr) = attr {
76-
check_needless_must_use(cx, sig.decl, item.owner_id, item.span, fn_header_span, attr);
76+
check_needless_must_use(cx, sig.decl, item.owner_id, item.span, fn_header_span, attr, sig);
7777
} else if let hir::TraitFn::Provided(eid) = *eid {
7878
let body = cx.tcx.hir().body(eid);
7979
if attr.is_none() && is_public && !is_proc_macro(attrs) {
@@ -98,6 +98,7 @@ fn check_needless_must_use(
9898
item_span: Span,
9999
fn_header_span: Span,
100100
attr: &Attribute,
101+
sig: &FnSig<'_>,
101102
) {
102103
if in_external_macro(cx.sess(), item_span) {
103104
return;
@@ -112,7 +113,7 @@ fn check_needless_must_use(
112113
diag.span_suggestion(attr.span, "remove the attribute", "", Applicability::MachineApplicable);
113114
},
114115
);
115-
} else if attr.value_str().is_none() && is_must_use_ty(cx, return_ty(cx, item_id)) {
116+
} else if attr.value_str().is_none() && is_must_use_ty(cx, return_ty(cx, item_id)) && !sig.header.is_async() {
116117
span_lint_and_help(
117118
cx,
118119
DOUBLE_MUST_USE,

tests/ui/double_must_use.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ async fn async_must_use() -> usize {
2727
unimplemented!();
2828
}
2929

30+
#[must_use]
31+
async fn async_must_use_result() -> Result<(), ()> {
32+
Ok(())
33+
}
34+
3035
fn main() {
3136
must_use_result();
3237
must_use_tuple();

0 commit comments

Comments
 (0)