diff options
| author | Mu42 <mu001999@outlook.com> | 2023-03-24 14:24:25 +0800 |
|---|---|---|
| committer | Mu42 <mu001999@outlook.com> | 2023-03-24 14:24:25 +0800 |
| commit | 8e56c2c5f1e8417ae7dcaf1fad1d72dd1ca964f5 (patch) | |
| tree | b7b6e9dc98ad234e4d4c760be769bb740320332e /compiler/rustc_lint/src | |
| parent | cf073ec2cbf0b90ded7893f429020b81dd28e6c3 (diff) | |
| download | rust-8e56c2c5f1e8417ae7dcaf1fad1d72dd1ca964f5.tar.gz rust-8e56c2c5f1e8417ae7dcaf1fad1d72dd1ca964f5.zip | |
Suggest ..= when someone tries to create an overflowing range
Diffstat (limited to 'compiler/rustc_lint/src')
| -rw-r--r-- | compiler/rustc_lint/src/types.rs | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 7ca50f5a2db..db93f6d3402 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -136,6 +136,13 @@ fn lint_overflowing_range_endpoint<'tcx>( expr: &'tcx hir::Expr<'tcx>, ty: &str, ) -> bool { + let (expr, cast_ty) = if let Node::Expr(par_expr) = cx.tcx.hir().get(cx.tcx.hir().parent_id(expr.hir_id)) + && let ExprKind::Cast(_, ty) = par_expr.kind { + (par_expr, Some(ty)) + } else { + (expr, None) + }; + // We only want to handle exclusive (`..`) ranges, // which are represented as `ExprKind::Struct`. let par_id = cx.tcx.hir().parent_id(expr.hir_id); @@ -157,13 +164,19 @@ fn lint_overflowing_range_endpoint<'tcx>( }; let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span) else { return false }; - use rustc_ast::{LitIntType, LitKind}; - let suffix = match lit.node { - LitKind::Int(_, LitIntType::Signed(s)) => s.name_str(), - LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str(), - LitKind::Int(_, LitIntType::Unsuffixed) => "", - _ => bug!(), + let suffix = if let Some(cast_ty) = cast_ty { + let Ok(ty) = cx.sess().source_map().span_to_snippet(cast_ty.span) else { return false }; + format!(" as {}", ty) + } else { + use rustc_ast::{LitIntType, LitKind}; + match lit.node { + LitKind::Int(_, LitIntType::Signed(s)) => s.name_str().to_owned(), + LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str().to_owned(), + LitKind::Int(_, LitIntType::Unsuffixed) => "".to_owned(), + _ => bug!(), + } }; + cx.emit_spanned_lint( OVERFLOWING_LITERALS, struct_expr.span, @@ -172,7 +185,7 @@ fn lint_overflowing_range_endpoint<'tcx>( suggestion: struct_expr.span, start, literal: lit_val - 1, - suffix, + suffix: &suffix, }, ); |
