Skip to content

Commit

Permalink
Fix false negative on Result<(), ()>
Browse files Browse the repository at this point in the history
  • Loading branch information
blyxyas committed Apr 3, 2023
1 parent d602743 commit a37eb4d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 11 additions & 1 deletion clippy_lints/src/functions/must_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rustc_ast::ast::Attribute;
use rustc_errors::Applicability;
use rustc_hir::def_id::DefIdSet;
use rustc_hir::{self as hir, def::Res, QPath};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LintContext};
use rustc_middle::{
lint::in_external_macro,
Expand Down Expand Up @@ -113,7 +114,16 @@ fn check_needless_must_use(
diag.span_suggestion(attr.span, "remove the attribute", "", Applicability::MachineApplicable);
},
);
} else if attr.value_str().is_none() && is_must_use_ty(cx, return_ty(cx, item_id)) && !sig.header.is_async() {
} else if attr.value_str().is_none() && is_must_use_ty(cx, return_ty(cx, item_id)) {
// Ignore async functions unless Future::Output type is a must_use type
if sig.header.is_async() {
let infcx = cx.tcx.infer_ctxt().build();
if let Some(future_ty) = infcx.get_impl_future_output_ty(return_ty(cx, item_id))
&& !is_must_use_ty(cx, future_ty) {
return;
}
}

span_lint_and_help(
cx,
DOUBLE_MUST_USE,
Expand Down
10 changes: 9 additions & 1 deletion tests/ui/double_must_use.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ LL | pub fn must_use_array() -> [Result<(), ()>; 1] {
|
= help: either add some descriptive text or remove the attribute

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

error: aborting due to 4 previous errors

0 comments on commit a37eb4d

Please sign in to comment.