diff options
| author | Yoshitomo Nakanishi <yurayura.rounin.3@gmail.com> | 2021-07-13 22:57:47 +0900 |
|---|---|---|
| committer | Yoshitomo Nakanishi <yurayura.rounin.3@gmail.com> | 2021-07-13 23:18:31 +0900 |
| commit | 25e4c7d73fd4c929208edf151c827f6c97318374 (patch) | |
| tree | eeb5c1b332b4217c4932a7779e9aa32214716e69 /clippy_lints/src/default_numeric_fallback.rs | |
| parent | 4e8cd4d346c13b0ef873f69c213ba96dc5bab5bf (diff) | |
| download | rust-25e4c7d73fd4c929208edf151c827f6c97318374.tar.gz rust-25e4c7d73fd4c929208edf151c827f6c97318374.zip | |
`default_numeric_fallback`: Add rustfix tests
Diffstat (limited to 'clippy_lints/src/default_numeric_fallback.rs')
| -rw-r--r-- | clippy_lints/src/default_numeric_fallback.rs | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/clippy_lints/src/default_numeric_fallback.rs b/clippy_lints/src/default_numeric_fallback.rs index ea28644b9a4..e719a1b0abf 100644 --- a/clippy_lints/src/default_numeric_fallback.rs +++ b/clippy_lints/src/default_numeric_fallback.rs @@ -1,5 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::source::snippet; +use clippy_utils::numeric_literal; +use clippy_utils::source::snippet_opt; use if_chain::if_chain; use rustc_ast::ast::{LitFloatType, LitIntType, LitKind}; use rustc_errors::Applicability; @@ -80,14 +81,23 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> { LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed)); if !ty_bound.is_numeric(); then { - let suffix = match lit_ty.kind() { - ty::Int(IntTy::I32) => "i32", - ty::Float(FloatTy::F64) => "f64", + let (suffix, is_float) = match lit_ty.kind() { + ty::Int(IntTy::I32) => ("i32", false), + ty::Float(FloatTy::F64) => ("f64", true), // Default numeric fallback never results in other types. _ => return, }; - let sugg = format!("{}_{}", snippet(self.cx, lit.span, ""), suffix); + let src = if let Some(src) = snippet_opt(self.cx, lit.span) { + src + } else { + match lit.node { + LitKind::Int(src, _) => format!("{}", src), + LitKind::Float(src, _) => format!("{}", src), + _ => return, + } + }; + let sugg = numeric_literal::format(&src, Some(suffix), is_float); span_lint_and_sugg( self.cx, DEFAULT_NUMERIC_FALLBACK, |
