Skip to content

Commit f4b701e

Browse files
committed
Fix false negative on Result<(), ()>
1 parent d602743 commit f4b701e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

clippy_lints/src/functions/must_use.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_ast::ast::Attribute;
33
use rustc_errors::Applicability;
44
use rustc_hir::def_id::DefIdSet;
55
use rustc_hir::{self as hir, def::Res, QPath};
6+
use rustc_infer::infer::TyCtxtInferExt;
67
use rustc_lint::{LateContext, LintContext};
78
use rustc_middle::{
89
lint::in_external_macro,
@@ -113,7 +114,16 @@ fn check_needless_must_use(
113114
diag.span_suggestion(attr.span, "remove the attribute", "", Applicability::MachineApplicable);
114115
},
115116
);
116-
} else if attr.value_str().is_none() && is_must_use_ty(cx, return_ty(cx, item_id)) && !sig.header.is_async() {
117+
} else if attr.value_str().is_none() && is_must_use_ty(cx, return_ty(cx, item_id)) {
118+
// Ignore async functions unless Future::Output type is a must_use type
119+
if sig.header.is_async() {
120+
let infcx = cx.tcx.infer_ctxt().build();
121+
if let Some(future_ty) = infcx.get_impl_future_output_ty(return_ty(cx, item_id))
122+
&& !is_must_use_ty(cx, future_ty) {
123+
return;
124+
}
125+
}
126+
117127
span_lint_and_help(
118128
cx,
119129
DOUBLE_MUST_USE,

tests/ui/double_must_use.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,13 @@ LL | pub fn must_use_array() -> [Result<(), ()>; 1] {
2323
|
2424
= help: either add some descriptive text or remove the attribute
2525

26-
error: aborting due to 3 previous errors
26+
error: this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]`
27+
--> $DIR/double_must_use.rs:31:1
28+
|
29+
LL | async fn async_must_use_result() -> Result<(), ()> {
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= help: either add some descriptive text or remove the attribute
33+
34+
error: aborting due to 4 previous errors
2735

0 commit comments

Comments
 (0)