Skip to content

Commit 81e72cc

Browse files
committed
special-case ranges
1 parent 6251b69 commit 81e72cc

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
2+
use clippy_utils::higher::Range;
23
use clippy_utils::source::SpanRangeExt;
34
use clippy_utils::ty::{expr_type_is_certain, has_drop};
45
use clippy_utils::{
@@ -347,6 +348,23 @@ fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<Vec
347348
ExprKind::Cast(inner, _) if expr_type_is_certain(cx, inner) => {
348349
reduce_expression(cx, inner).or_else(|| Some(vec![inner]))
349350
},
351+
// In the normal `Struct` case, we bail out if any of the fields has an uncertain type.
352+
// But for two-sided ranges, we know that if the type of one of the sides is certain, then so is the other
353+
// one's. So we only check that, more relaxed pre-condition.
354+
//
355+
// Note that that condition true in general for any struct with a generic present in two fields, but
356+
// generalizing the check to those would be cumbersome.
357+
ExprKind::Struct(..)
358+
if let Some(range) = Range::hir(expr)
359+
&& let Some(start) = range.start
360+
&& let Some(end) = range.end =>
361+
{
362+
if [start, end].into_iter().any(|e| expr_type_is_certain(cx, e)) {
363+
Some([start, end].into_iter().collect())
364+
} else {
365+
None
366+
}
367+
},
350368
ExprKind::Struct(_, fields, ref base) => {
351369
if fields.iter().any(|f| !expr_type_is_certain(cx, &f.expr))
352370
|| has_drop(cx, cx.typeck_results().expr_ty(expr))

tests/ui/unnecessary_operation.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ error: unnecessary operation
6767
LL | ..get_number();
6868
| ^^^^^^^^^^^^^^^ help: statement can be reduced to: `get_number();`
6969

70+
error: unnecessary operation
71+
--> tests/ui/unnecessary_operation.rs:93:5
72+
|
73+
LL | 5..get_number();
74+
| ^^^^^^^^^^^^^^^^ help: statement can be reduced to: `5;get_number();`
75+
7076
error: unnecessary operation
7177
--> tests/ui/unnecessary_operation.rs:95:5
7278
|
@@ -112,5 +118,5 @@ error: unnecessary operation
112118
LL | [42, 55][get_usize()];
113119
| ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`
114120

115-
error: aborting due to 18 previous errors
121+
error: aborting due to 19 previous errors
116122

0 commit comments

Comments
 (0)