|
| 1 | +use clippy_config::msrvs::{Msrv, NUMERIC_ASSOCIATED_CONSTANTS}; |
| 2 | +use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then}; |
| 3 | +use clippy_utils::source::snippet_opt; |
| 4 | +use clippy_utils::{get_parent_expr, is_from_proc_macro, last_path_segment, std_or_core}; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::def::{DefKind, Res}; |
| 7 | +use rustc_hir::{ExprKind, Item, ItemKind, PrimTy, QPath, TyKind, UseKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 9 | +use rustc_middle::lint::in_external_macro; |
| 10 | +use rustc_session::impl_lint_pass; |
| 11 | +use rustc_span::symbol::kw; |
| 12 | +use rustc_span::{sym, Symbol}; |
| 13 | + |
| 14 | +declare_clippy_lint! { |
| 15 | + /// ### What it does |
| 16 | + /// Checks for usage of `<integer>::max_value()`, `std::<integer>::MAX`, |
| 17 | + /// `std::<float>::EPSILON`, etc. |
| 18 | + /// |
| 19 | + /// ### Why is this bad? |
| 20 | + /// All of these have been superceded by the associated constants on their respective types, |
| 21 | + /// such as `i128::MAX`. These legacy items may be deprecated in a future version of rust. |
| 22 | + /// |
| 23 | + /// ### Example |
| 24 | + /// ```rust |
| 25 | + /// let eps = std::f32::EPSILON; |
| 26 | + /// ``` |
| 27 | + /// Use instead: |
| 28 | + /// ```rust |
| 29 | + /// let eps = f32::EPSILON; |
| 30 | + /// ``` |
| 31 | + #[clippy::version = "1.72.0"] |
| 32 | + pub LEGACY_NUMERIC_CONSTANTS, |
| 33 | + style, |
| 34 | + "checks for usage of legacy std numeric constants and methods" |
| 35 | +} |
| 36 | +pub struct LegacyNumericConstants { |
| 37 | + msrv: Msrv, |
| 38 | +} |
| 39 | + |
| 40 | +impl LegacyNumericConstants { |
| 41 | + #[must_use] |
| 42 | + pub fn new(msrv: Msrv) -> Self { |
| 43 | + Self { msrv } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl_lint_pass!(LegacyNumericConstants => [LEGACY_NUMERIC_CONSTANTS]); |
| 48 | + |
| 49 | +impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants { |
| 50 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 51 | + let Self { msrv } = self; |
| 52 | + |
| 53 | + if msrv.meets(NUMERIC_ASSOCIATED_CONSTANTS) |
| 54 | + && !in_external_macro(cx.sess(), item.span) |
| 55 | + && let ItemKind::Use(path, kind @ (UseKind::Single | UseKind::Glob)) = item.kind |
| 56 | + // These modules are "TBD" deprecated, and the contents are too, so lint on the `use` |
| 57 | + // statement directly |
| 58 | + && let Some(def_id) = path.res[0].opt_def_id() |
| 59 | + && let def_path = cx.get_def_path(def_id) |
| 60 | + && is_path_in_numeric_module(&def_path, true) |
| 61 | + { |
| 62 | + let plurality = matches!( |
| 63 | + kind, |
| 64 | + UseKind::Glob | UseKind::Single if matches!(path.res[0], Res::Def(DefKind::Mod, _)), |
| 65 | + ); |
| 66 | + |
| 67 | + span_lint_and_then( |
| 68 | + cx, |
| 69 | + LEGACY_NUMERIC_CONSTANTS, |
| 70 | + path.span, |
| 71 | + if plurality { |
| 72 | + "importing legacy numeric constants" |
| 73 | + } else { |
| 74 | + "importing a legacy numeric constant" |
| 75 | + }, |
| 76 | + |diag| { |
| 77 | + if item.ident.name == kw::Underscore { |
| 78 | + diag.help("remove this import"); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if plurality && let [.., module_name] = &*def_path { |
| 83 | + diag.help( |
| 84 | + "remove this import and use the associated constants on the primitive type instead" |
| 85 | + ) |
| 86 | + .note(format!( |
| 87 | + "then `{module_name}::<CONST>` will resolve to the respective associated constant" |
| 88 | + )); |
| 89 | + } else if let [.., module_name, name] = &*def_path { |
| 90 | + diag.help( |
| 91 | + format!("remove this import and use the associated constant `{module_name}::{name}` from the primitive type instead") |
| 92 | + ); |
| 93 | + } |
| 94 | + }, |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) { |
| 100 | + let Self { msrv } = self; |
| 101 | + |
| 102 | + if !msrv.meets(NUMERIC_ASSOCIATED_CONSTANTS) || in_external_macro(cx.sess(), expr.span) { |
| 103 | + return; |
| 104 | + } |
| 105 | + let ExprKind::Path(qpath) = expr.kind else { |
| 106 | + return; |
| 107 | + }; |
| 108 | + |
| 109 | + // `std::<integer>::<CONST>` check |
| 110 | + let (span, sugg, msg) = if let QPath::Resolved(None, path) = qpath |
| 111 | + && let Some(def_id) = path.res.opt_def_id() |
| 112 | + && let path = cx.get_def_path(def_id) |
| 113 | + && is_path_in_numeric_module(&path, false) |
| 114 | + && let [.., module_name, name] = &*path |
| 115 | + && let Some(snippet) = snippet_opt(cx, expr.span) |
| 116 | + && let is_float_module = (*module_name == sym::f32 || *module_name == sym::f64) |
| 117 | + // Skip linting if this usage looks identical to the associated constant, since this |
| 118 | + // would only require removing a `use` import. We don't ignore ones from `f32` or `f64`, however. |
| 119 | + && let identical = snippet == format!("{module_name}::{name}") |
| 120 | + && (!identical || is_float_module) |
| 121 | + { |
| 122 | + ( |
| 123 | + expr.span, |
| 124 | + if identical { |
| 125 | + None |
| 126 | + } else { |
| 127 | + Some(format!("{module_name}::{name}")) |
| 128 | + }, |
| 129 | + "usage of a legacy numeric constant", |
| 130 | + ) |
| 131 | + // `<integer>::xxx_value` check |
| 132 | + } else if let QPath::TypeRelative(ty, _) = qpath |
| 133 | + && let TyKind::Path(ty_qpath) = ty.kind |
| 134 | + && let Res::PrimTy(PrimTy::Int(_) | PrimTy::Uint(_)) = cx.qpath_res(&ty_qpath, ty.hir_id) |
| 135 | + && let last_segment = last_path_segment(&qpath) |
| 136 | + && let name = last_segment.ident.name.as_str() |
| 137 | + && (name == "max_value" || name == "min_value") |
| 138 | + // Also remove the `()` |
| 139 | + && let Some(par_expr) = get_parent_expr(cx, expr) |
| 140 | + && let ExprKind::Call(_, _) = par_expr.kind |
| 141 | + { |
| 142 | + ( |
| 143 | + last_segment.ident.span.with_hi(par_expr.span.hi()), |
| 144 | + Some(name[..=2].to_ascii_uppercase()), |
| 145 | + "usage of a legacy numeric method", |
| 146 | + ) |
| 147 | + } else { |
| 148 | + return; |
| 149 | + }; |
| 150 | + |
| 151 | + if is_from_proc_macro(cx, expr) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + span_lint_hir_and_then(cx, LEGACY_NUMERIC_CONSTANTS, expr.hir_id, span, msg, |diag| { |
| 156 | + if let Some(sugg) = sugg { |
| 157 | + diag.span_suggestion( |
| 158 | + span, |
| 159 | + "use the associated constant instead", |
| 160 | + sugg, |
| 161 | + Applicability::MaybeIncorrect, |
| 162 | + ); |
| 163 | + } else if let Some(std_or_core) = std_or_core(cx) |
| 164 | + && let QPath::Resolved(None, path) = qpath |
| 165 | + { |
| 166 | + diag.help(format!( |
| 167 | + "remove the import that brings `{std_or_core}::{}` into scope", |
| 168 | + // Must be `<module>::<CONST>` if `needs_import_removed` is true yet is |
| 169 | + // being linted |
| 170 | + path.segments[0].ident.name, |
| 171 | + )); |
| 172 | + } |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | + extract_msrv_attr!(LateContext); |
| 177 | +} |
| 178 | + |
| 179 | +fn is_path_in_numeric_module(path: &[Symbol], ignore_float_modules: bool) -> bool { |
| 180 | + if let [ |
| 181 | + sym::core, |
| 182 | + module @ (sym::u8 |
| 183 | + | sym::i8 |
| 184 | + | sym::u16 |
| 185 | + | sym::i16 |
| 186 | + | sym::u32 |
| 187 | + | sym::i32 |
| 188 | + | sym::u64 |
| 189 | + | sym::i64 |
| 190 | + | sym::u128 |
| 191 | + | sym::i128 |
| 192 | + | sym::usize |
| 193 | + | sym::isize |
| 194 | + | sym::f32 |
| 195 | + | sym::f64), |
| 196 | + .., |
| 197 | + ] = path |
| 198 | + && !path.get(2).is_some_and(|&s| s == sym!(consts)) |
| 199 | + { |
| 200 | + // If `ignore_float_modules` is `true`, return `None` for `_::f32` or `_::f64`, but not |
| 201 | + // _::f64::MAX` or similar. |
| 202 | + if ignore_float_modules && (*module == sym::f32 || *module == sym::f64) && path.get(2).is_none() { |
| 203 | + return false; |
| 204 | + } |
| 205 | + |
| 206 | + return true; |
| 207 | + } |
| 208 | + |
| 209 | + false |
| 210 | +} |
0 commit comments