Skip to content
Open
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
53 changes: 41 additions & 12 deletions crates/hir-ty/src/diagnostics/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,19 +330,20 @@ impl<'db> ExprValidator<'db> {
let (Expr::Block { statements, .. } | Expr::Unsafe { statements, .. }) = expr else {
return;
};
if self.validate_lints {
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 May 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you separate to a new loop? And why this if?

View changes since the review

for stmt in &**statements {
if let Statement::Expr { expr: stmt_expr, has_semi: true } = *stmt {
self.check_unused_must_use(stmt_expr);
}
}
}
let pattern_arena = Arena::new();
let cx = MatchCheckCtx::new(self.owner.module(self.db()), &self.infcx, self.env);
for stmt in &**statements {
let diag = match *stmt {
Statement::Expr { expr: stmt_expr, has_semi: true } if self.validate_lints => {
self.check_unused_must_use(stmt_expr)
}
Statement::Let { pat, initializer, else_branch: None, .. } => {
if let Statement::Let { pat, initializer, else_branch: None, .. } = *stmt
&& let Some(diag) =
self.check_non_exhaustive_let(&cx, &pattern_arena, pat, initializer)
}
_ => None,
};
if let Some(diag) = diag {
{
self.diagnostics.push(diag);
}
}
Expand Down Expand Up @@ -415,7 +416,33 @@ impl<'db> ExprValidator<'db> {
pattern
}

fn check_unused_must_use(&self, expr: ExprId) -> Option<BodyValidationDiagnostic<'db>> {
fn check_unused_must_use(&mut self, mut expr: ExprId) {
// Walk through container expressions so that the value-producing leaf is
// checked even when wrapped in a block, `unsafe { .. }`, `if`/`match`, or
// a `const { .. }` block. Single-tail chains are followed by reassigning
// `expr`; branching containers (`if`/`match`) recurse on each arm.
loop {
match &self.body[expr] {
Expr::Block { tail: Some(tail), .. }
| Expr::Unsafe { tail: Some(tail), .. }
| Expr::Const(tail) => expr = *tail,
Expr::If { then_branch, else_branch, .. } => {
self.check_unused_must_use(*then_branch);
if let Some(else_branch) = else_branch {
self.check_unused_must_use(*else_branch);
}
return;
}
Expr::Match { arms, .. } => {
for arm in arms.iter() {
self.check_unused_must_use(arm.expr);
}
return;
}
_ => break,
}
}

let fn_def = match &self.body[expr] {
Expr::Call { callee, .. } => {
let callee_ty = self.infer.expr_ty(*callee);
Expand All @@ -430,7 +457,7 @@ impl<'db> ExprValidator<'db> {
Expr::MethodCall { .. } => {
self.infer.method_resolution(expr).map(|(func, _)| func.into())
}
_ => return None,
_ => None,
};
let ty_def = self.infer.type_of_expr_with_adjust(expr).and_then(|ty| match ty.kind() {
TyKind::Adt(adt, _) => Some(adt.def_id().into()),
Expand All @@ -440,7 +467,9 @@ impl<'db> ExprValidator<'db> {
AttrFlags::must_use_message(self.db(), owner?)
.map(|message| BodyValidationDiagnostic::UnusedMustUse { expr, message })
};
must_use_diag(fn_def).or_else(|| must_use_diag(ty_def))
if let Some(diag) = must_use_diag(fn_def).or_else(|| must_use_diag(ty_def)) {
self.diagnostics.push(diag);
}
}

fn check_for_trailing_return(&mut self, body_expr: ExprId, body: &Body) {
Expand Down
192 changes: 192 additions & 0 deletions crates/ide-diagnostics/src/handlers/unused_must_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,198 @@ fn main() {
x = produces();
let _ = x;
}
"#,
);
}

#[test]
fn block_tail_expression_in_stmt_position() {
check_diagnostics(
r#"
#[must_use]
fn produces() -> i32 { 0 }
fn main() {
{
produces()
//^^^^^^^^^^ warn: unused return value that must be used
};
}
"#,
);
}

#[test]
fn unsafe_block_tail_expression_in_stmt_position() {
check_diagnostics(
r#"
#[must_use]
unsafe fn produces() -> i32 { 0 }
fn main() {
unsafe {
produces()
//^^^^^^^^^^ warn: unused return value that must be used
};
}
"#,
);
}

#[test]
fn nested_block_tail_expression() {
check_diagnostics(
r#"
#[must_use]
fn produces() -> i32 { 0 }
fn main() {
{
{
produces()
//^^^^^^^^^^ warn: unused return value that must be used
}
};
}
"#,
);
}

#[test]
fn no_warning_when_block_tail_is_bound() {
check_diagnostics(
r#"
#[must_use]
fn produces() -> i32 { 0 }
fn main() {
let _x = {
produces()
};
}
"#,
);
}

#[test]
fn if_branches_in_stmt_position() {
check_diagnostics(
r#"
#[must_use]
fn produces() -> i32 { 0 }
fn main() {
if true {
produces()
//^^^^^^^^^^ warn: unused return value that must be used
} else {
produces()
//^^^^^^^^^^ warn: unused return value that must be used
};
}
"#,
);
}

#[test]
fn match_arms_in_stmt_position() {
check_diagnostics(
r#"
#[must_use]
fn produces() -> i32 { 0 }
fn main() {
match 0 {
0 => produces(),
//^^^^^^^^^^ warn: unused return value that must be used
_ => produces(),
//^^^^^^^^^^ warn: unused return value that must be used
};
}
"#,
);
}

#[test]
fn const_block_in_stmt_position() {
check_diagnostics(
r#"
#[must_use]
const fn produces() -> i32 { 0 }
fn main() {
const {
produces()
//^^^^^^^^^^ warn: unused return value that must be used
};
}
"#,
);
}

#[test]
fn must_use_type_through_block() {
check_diagnostics(
r#"
#[must_use]
struct Important;
fn produces() -> Important { Important }
fn main() {
{
produces()
//^^^^^^^^^^ warn: unused return value that must be used
};
}
"#,
);
}

#[test]
fn allow_attribute_on_fn_silences_lint() {
check_diagnostics(
r#"
#[must_use]
fn produces() -> i32 { 0 }
#[allow(unused_must_use)]
fn main() {
produces();
}
"#,
);
}

#[test]
fn allow_attribute_on_stmt_silences_lint() {
check_diagnostics(
r#"
#[must_use]
fn produces() -> i32 { 0 }
fn main() {
#[allow(unused_must_use)]
produces();
}
"#,
);
}

#[test]
fn allow_unused_group_silences_lint() {
check_diagnostics(
r#"
#[must_use]
fn produces() -> i32 { 0 }
#[allow(unused)]
fn main() {
produces();
}
"#,
);
}

#[test]
fn deny_attribute_escalates_lint() {
check_diagnostics(
r#"
#[must_use]
fn produces() -> i32 { 0 }
#[deny(unused_must_use)]
fn main() {
produces();
//^^^^^^^^^^ error: unused return value that must be used
}
"#,
);
}
Expand Down
Loading