diff options
| author | yukang <moorekang@gmail.com> | 2025-02-09 12:20:39 +0800 |
|---|---|---|
| committer | yukang <moorekang@gmail.com> | 2025-02-09 20:39:43 +0800 |
| commit | ace6bb9869747b50d7d4bfaacc65d0592ec94aef (patch) | |
| tree | f16733be0c437575365605487982d4bc588832b3 /compiler/rustc_lint | |
| parent | 43ca9d18e333797f0aa3b525501a7cec8d61a96b (diff) | |
| download | rust-ace6bb9869747b50d7d4bfaacc65d0592ec94aef.tar.gz rust-ace6bb9869747b50d7d4bfaacc65d0592ec94aef.zip | |
Fix unwrap error in overflowing int literal
Diffstat (limited to 'compiler/rustc_lint')
| -rw-r--r-- | compiler/rustc_lint/src/types.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_lint/src/types/literal.rs | 5 |
2 files changed, 7 insertions, 4 deletions
diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 80007f34db3..9cf5d0f460b 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -543,7 +543,11 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits { lit: &'tcx hir::Lit, negated: bool, ) { - lint_literal(cx, self, hir_id, lit.span, lit, negated) + if negated { + self.negated_expr_id = Some(hir_id); + self.negated_expr_span = Some(lit.span); + } + lint_literal(cx, self, hir_id, lit.span, lit, negated); } fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'tcx>) { diff --git a/compiler/rustc_lint/src/types/literal.rs b/compiler/rustc_lint/src/types/literal.rs index 71e6e229907..287344f9693 100644 --- a/compiler/rustc_lint/src/types/literal.rs +++ b/compiler/rustc_lint/src/types/literal.rs @@ -245,12 +245,11 @@ fn lint_int_literal<'tcx>( lit: &hir::Lit, t: ty::IntTy, v: u128, - negated: bool, ) { let int_type = t.normalize(cx.sess().target.pointer_width); let (min, max) = int_ty_range(int_type); let max = max as u128; - let negative = negated ^ (type_limits.negated_expr_id == Some(hir_id)); + let negative = type_limits.negated_expr_id == Some(hir_id); // Detect literal value out of range [min, max] inclusive // avoiding use of -min to prevent overflow/panic @@ -366,7 +365,7 @@ pub(crate) fn lint_literal<'tcx>( ty::Int(t) => { match lit.node { ast::LitKind::Int(v, ast::LitIntType::Signed(_) | ast::LitIntType::Unsuffixed) => { - lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get(), negated) + lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get()) } _ => bug!(), }; |
