|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::{Expr, ExprKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_middle::ty::{self, Ty}; |
| 7 | +use rustc_session::declare_lint_pass; |
| 8 | +use rustc_span::symbol::sym; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// |
| 15 | + /// ### Example |
| 16 | + /// ```no_run |
| 17 | + /// // example code where clippy issues a warning |
| 18 | + /// ``` |
| 19 | + /// Use instead: |
| 20 | + /// ```no_run |
| 21 | + /// // example code which does not raise clippy warning |
| 22 | + /// ``` |
| 23 | + #[clippy::version = "1.81.0"] |
| 24 | + pub NON_ZERO_SUGGESTIONS, |
| 25 | + restriction, |
| 26 | + "suggests using `NonZero#` from `u#` or `i#` for more efficient and type-safe conversions" |
| 27 | +} |
| 28 | + |
| 29 | +declare_lint_pass!(NonZeroSuggestions => [NON_ZERO_SUGGESTIONS]); |
| 30 | + |
| 31 | +impl<'tcx> LateLintPass<'tcx> for NonZeroSuggestions { |
| 32 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 33 | + if let ExprKind::Call(func, [arg]) = expr.kind { |
| 34 | + if let ExprKind::Path(qpath) = &func.kind { |
| 35 | + if let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() { |
| 36 | + let fn_name = cx.tcx.item_name(def_id); |
| 37 | + let target_ty = cx.typeck_results().expr_ty(expr); |
| 38 | + |
| 39 | + if let ExprKind::MethodCall(rcv_path, receiver, _, _) = &arg.kind { |
| 40 | + let receiver_ty = cx.typeck_results().expr_ty(receiver); |
| 41 | + if let ty::Adt(adt_def, _) = receiver_ty.kind() { |
| 42 | + if adt_def.is_struct() && cx.tcx.get_diagnostic_name(adt_def.did()) == Some(sym::NonZero) { |
| 43 | + if let Some(target_non_zero_type) = get_target_non_zero_type(target_ty) { |
| 44 | + let arg_snippet = get_arg_snippet(cx, arg, rcv_path); |
| 45 | + suggest_non_zero_conversion(cx, expr, fn_name, target_non_zero_type, &arg_snippet); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn get_arg_snippet(cx: &LateContext<'_>, arg: &Expr<'_>, rcv_path: &rustc_hir::PathSegment<'_>) -> String { |
| 57 | + let arg_snippet = snippet(cx, arg.span, ".."); |
| 58 | + if let Some(index) = arg_snippet.rfind(&format!(".{}", rcv_path.ident.name)) { |
| 59 | + arg_snippet[..index].trim().to_string() |
| 60 | + } else { |
| 61 | + arg_snippet.to_string() |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn suggest_non_zero_conversion( |
| 66 | + cx: &LateContext<'_>, |
| 67 | + expr: &Expr<'_>, |
| 68 | + fn_name: rustc_span::Symbol, |
| 69 | + target_non_zero_type: &str, |
| 70 | + arg_snippet: &str, |
| 71 | +) { |
| 72 | + let suggestion = format!("{}::{}({})", target_non_zero_type, fn_name, arg_snippet); |
| 73 | + span_lint_and_sugg( |
| 74 | + cx, |
| 75 | + NON_ZERO_SUGGESTIONS, |
| 76 | + expr.span, |
| 77 | + format!( |
| 78 | + "Consider using `{}::{}()` for more efficient and type-safe conversion", |
| 79 | + target_non_zero_type, fn_name |
| 80 | + ), |
| 81 | + "Replace with", |
| 82 | + suggestion, |
| 83 | + Applicability::MachineApplicable, |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +fn get_target_non_zero_type(ty: Ty<'_>) -> Option<&'static str> { |
| 88 | + match ty.kind() { |
| 89 | + ty::Uint(uint_ty) => Some(match uint_ty { |
| 90 | + ty::UintTy::U8 => "NonZeroU8", |
| 91 | + ty::UintTy::U16 => "NonZeroU16", |
| 92 | + ty::UintTy::U32 => "NonZeroU32", |
| 93 | + ty::UintTy::U64 => "NonZeroU64", |
| 94 | + ty::UintTy::U128 => "NonZeroU128", |
| 95 | + ty::UintTy::Usize => "NonZeroUsize", |
| 96 | + }), |
| 97 | + ty::Int(int_ty) => Some(match int_ty { |
| 98 | + ty::IntTy::I8 => "NonZeroI8", |
| 99 | + ty::IntTy::I16 => "NonZeroI16", |
| 100 | + ty::IntTy::I32 => "NonZeroI32", |
| 101 | + ty::IntTy::I64 => "NonZeroI64", |
| 102 | + ty::IntTy::I128 => "NonZeroI128", |
| 103 | + ty::IntTy::Isize => "NonZeroIsize", |
| 104 | + }), |
| 105 | + _ => None, |
| 106 | + } |
| 107 | +} |
0 commit comments