Skip to content

Commit 23574ee

Browse files
committed
Add ExprKind::Block to type certainty algorithm
1 parent 69a6e94 commit 23574ee

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

clippy_utils/src/ty/type_certainty/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ fn expr_type_certainty(cx: &LateContext<'_>, expr: &Expr<'_>, in_arg: bool) -> C
110110

111111
ExprKind::Struct(qpath, _, _) => qpath_certainty(cx, qpath, true),
112112

113+
ExprKind::Block(block, _) => block
114+
.expr
115+
.map_or(Certainty::Certain(None), |expr| expr_type_certainty(cx, expr, false)),
116+
113117
_ => Certainty::Uncertain,
114118
};
115119

tests/ui/unnecessary_cast.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,10 @@ mod fixable {
304304
f(((1 + 2u32)));
305305
//~^ unnecessary_cast
306306
}
307+
308+
fn with_blocks(a: i64, b: i64, c: u64) {
309+
let threshold = if c < 10 { a } else { b };
310+
let _ = threshold;
311+
//~^ unnecessary_cast
312+
}
307313
}

tests/ui/unnecessary_cast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,10 @@ mod fixable {
304304
f((1 + 2u32) as u32);
305305
//~^ unnecessary_cast
306306
}
307+
308+
fn with_blocks(a: i64, b: i64, c: u64) {
309+
let threshold = if c < 10 { a } else { b };
310+
let _ = threshold as i64;
311+
//~^ unnecessary_cast
312+
}
307313
}

tests/ui/unnecessary_cast.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,11 @@ error: casting to the same type is unnecessary (`u32` -> `u32`)
277277
LL | f((1 + 2u32) as u32);
278278
| ^^^^^^^^^^^^^^^^^ help: try: `((1 + 2u32))`
279279

280-
error: aborting due to 46 previous errors
280+
error: casting to the same type is unnecessary (`i64` -> `i64`)
281+
--> tests/ui/unnecessary_cast.rs:310:17
282+
|
283+
LL | let _ = threshold as i64;
284+
| ^^^^^^^^^^^^^^^^ help: try: `threshold`
285+
286+
error: aborting due to 47 previous errors
281287

0 commit comments

Comments
 (0)