Skip to content

Commit 76e59b9

Browse files
committed
special-case ranges
1 parent fe28c1b commit 76e59b9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 21 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::res::MaybeResPath;
34
use clippy_utils::source::SpanRangeExt;
45
use clippy_utils::ty::{expr_type_is_certain, has_drop};
@@ -354,6 +355,26 @@ fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(Ve
354355
ExprKind::Cast(inner, _) if expr_type_is_certain(cx, inner) => {
355356
reduce_expression(cx, inner).or_else(|| Some((vec![inner], Applicability::MachineApplicable)))
356357
},
358+
// In the normal `Struct` case, we bail out if any of the fields has an uncertain type.
359+
// But for two-sided ranges, we know that if the type of one of the sides is certain, then so is the other
360+
// one's. So we only check that, more relaxed pre-condition.
361+
//
362+
// Note that that condition true in general for any struct with a generic present in two fields, but
363+
// generalizing the check to those would be cumbersome.
364+
ExprKind::Struct(..)
365+
if let Some(range) = Range::hir(cx, expr)
366+
&& let Some(start) = range.start
367+
&& let Some(end) = range.end =>
368+
{
369+
let applicability = if [start, end].into_iter().any(|e| expr_type_is_certain(cx, e)) {
370+
Applicability::MachineApplicable
371+
} else {
372+
// there's a risk that if we take the field exprs out of the context of the range constructor,
373+
// their types might become ambiguous
374+
Applicability::MaybeIncorrect
375+
};
376+
Some((vec![start, end], applicability))
377+
},
357378
ExprKind::Struct(_, fields, ref base) => {
358379
let applicability = if fields.iter().all(|f| expr_type_is_certain(cx, f.expr)) {
359380
Applicability::MachineApplicable

0 commit comments

Comments
 (0)