Skip to content

Commit 16c3b64

Browse files
authored
Rollup merge of rust-lang#102602 - WaffleLapkin:linty_action, r=estebank
Slightly tweak comments wrt `lint_overflowing_range_endpoint` From the review: rust-lang#101986 (comment) It _seemed_ that the lint was not emitted when the `if` check failed, but _actually_ this happens already in a special case and the lint is emitted outside of this function, if this function doesn't. I've cleared up the code/comments a bit, so it's more obvious :) r? ```@estebank```
2 parents 26c96e3 + 64e7fd9 commit 16c3b64

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

compiler/rustc_lint/src/types.rs

+40-38
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ impl TypeLimits {
116116
}
117117
}
118118

119-
/// Attempts to special-case the overflowing literal lint when it occurs as a range endpoint.
120-
/// Returns `true` iff the lint was overridden.
119+
/// Attempts to special-case the overflowing literal lint when it occurs as a range endpoint (`expr..MAX+1`).
120+
/// Returns `true` iff the lint was emitted.
121121
fn lint_overflowing_range_endpoint<'tcx>(
122122
cx: &LateContext<'tcx>,
123123
lit: &hir::Lit,
@@ -140,44 +140,46 @@ fn lint_overflowing_range_endpoint<'tcx>(
140140
return false;
141141
}
142142

143-
let mut overwritten = false;
144143
// We can suggest using an inclusive range
145144
// (`..=`) instead only if it is the `end` that is
146145
// overflowing and only by 1.
147-
if eps[1].expr.hir_id == expr.hir_id && lit_val - 1 == max
148-
&& let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span)
149-
{
150-
cx.struct_span_lint(
151-
OVERFLOWING_LITERALS,
152-
struct_expr.span,
153-
fluent::lint::range_endpoint_out_of_range,
154-
|lint| {
155-
use ast::{LitIntType, LitKind};
156-
157-
lint.set_arg("ty", ty);
158-
159-
// We need to preserve the literal's suffix,
160-
// as it may determine typing information.
161-
let suffix = match lit.node {
162-
LitKind::Int(_, LitIntType::Signed(s)) => s.name_str(),
163-
LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str(),
164-
LitKind::Int(_, LitIntType::Unsuffixed) => "",
165-
_ => bug!(),
166-
};
167-
let suggestion = format!("{}..={}{}", start, lit_val - 1, suffix);
168-
lint.span_suggestion(
169-
struct_expr.span,
170-
fluent::lint::suggestion,
171-
suggestion,
172-
Applicability::MachineApplicable,
173-
);
174-
overwritten = true;
146+
if !(eps[1].expr.hir_id == expr.hir_id && lit_val - 1 == max) {
147+
return false;
148+
};
149+
let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span) else { return false };
175150

176-
lint
177-
},
178-
);
179-
}
180-
overwritten
151+
cx.struct_span_lint(
152+
OVERFLOWING_LITERALS,
153+
struct_expr.span,
154+
fluent::lint::range_endpoint_out_of_range,
155+
|lint| {
156+
use ast::{LitIntType, LitKind};
157+
158+
lint.set_arg("ty", ty);
159+
160+
// We need to preserve the literal's suffix,
161+
// as it may determine typing information.
162+
let suffix = match lit.node {
163+
LitKind::Int(_, LitIntType::Signed(s)) => s.name_str(),
164+
LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str(),
165+
LitKind::Int(_, LitIntType::Unsuffixed) => "",
166+
_ => bug!(),
167+
};
168+
let suggestion = format!("{}..={}{}", start, lit_val - 1, suffix);
169+
lint.span_suggestion(
170+
struct_expr.span,
171+
fluent::lint::suggestion,
172+
suggestion,
173+
Applicability::MachineApplicable,
174+
);
175+
176+
lint
177+
},
178+
);
179+
180+
// We've just emitted a lint, special cased for `(...)..MAX+1` ranges,
181+
// return `true` so the callers don't also emit a lint
182+
true
181183
}
182184

183185
// For `isize` & `usize`, be conservative with the warnings, so that the
@@ -358,7 +360,7 @@ fn lint_int_literal<'tcx>(
358360
}
359361

360362
if lint_overflowing_range_endpoint(cx, lit, v, max, e, t.name_str()) {
361-
// The overflowing literal lint was overridden.
363+
// The overflowing literal lint was emited by `lint_overflowing_range_endpoint`.
362364
return;
363365
}
364366

@@ -427,7 +429,7 @@ fn lint_uint_literal<'tcx>(
427429
}
428430
}
429431
if lint_overflowing_range_endpoint(cx, lit, lit_val, max, e, t.name_str()) {
430-
// The overflowing literal lint was overridden.
432+
// The overflowing literal lint was emited by `lint_overflowing_range_endpoint`.
431433
return;
432434
}
433435
if let Some(repr_str) = get_bin_hex_repr(cx, lit) {

0 commit comments

Comments
 (0)