|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_sugg, is_from_proc_macro, is_lang_item_or_ctor, last_path_segment}; |
| 2 | +use rustc_errors::Applicability; |
| 3 | +use rustc_hir::{Expr, ExprKind, LangItem, QPath}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 5 | +use rustc_middle::{ |
| 6 | + lint::in_external_macro, |
| 7 | + ty::{self, Ty}, |
| 8 | +}; |
| 9 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 10 | +use rustc_span::symbol::kw; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks for types constructed by `default` that really don't need to be. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// It's harder for the reader to know what the value is, and it's an unnecessary function call. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// ```rust,ignore |
| 21 | + /// let a = A(Option::default()); |
| 22 | + /// ``` |
| 23 | + /// Use instead: |
| 24 | + /// ```rust,ignore |
| 25 | + /// let a = A(None); |
| 26 | + /// ``` |
| 27 | + #[clippy::version = "1.72.0"] |
| 28 | + pub TRIVIAL_DEFAULT_CONSTRUCTED_TYPES, |
| 29 | + pedantic, |
| 30 | + "checks for usage of `Default::default` to construct trivial types" |
| 31 | +} |
| 32 | +declare_lint_pass!(TrivialDefaultConstructedTypes => [TRIVIAL_DEFAULT_CONSTRUCTED_TYPES]); |
| 33 | + |
| 34 | +impl<'tcx> LateLintPass<'tcx> for TrivialDefaultConstructedTypes { |
| 35 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 36 | + if !in_external_macro(cx.sess(), expr.span) |
| 37 | + && let ExprKind::Call(call, _) = expr.kind |
| 38 | + && let ExprKind::Path(qpath) = call.kind |
| 39 | + // `last_path_segment` ICEs if we give it a `LangItem`. |
| 40 | + && !matches!(qpath, QPath::LangItem(..)) |
| 41 | + && last_path_segment(&qpath).ident.name == kw::Default |
| 42 | + { |
| 43 | + let ret_ty = cx |
| 44 | + .typeck_results() |
| 45 | + .expr_ty(call) |
| 46 | + .fn_sig(cx.tcx) |
| 47 | + .output() |
| 48 | + .skip_binder() |
| 49 | + .peel_refs(); |
| 50 | + if let Some(default) = default_value(cx, ret_ty) && !is_from_proc_macro(cx, expr) { |
| 51 | + span_lint_and_sugg( |
| 52 | + cx, |
| 53 | + TRIVIAL_DEFAULT_CONSTRUCTED_TYPES, |
| 54 | + expr.span, |
| 55 | + "constructing a trivial type using `default`", |
| 56 | + "try", |
| 57 | + default.to_string(), |
| 58 | + Applicability::MachineApplicable, |
| 59 | + ); |
| 60 | + } else if let ty::Tuple(fields) = ret_ty.kind() |
| 61 | + && let Some(fields_default) = fields.iter() |
| 62 | + .map(|field| default_value(cx, field)) |
| 63 | + .collect::<Option<Vec<&'static str>>>() |
| 64 | + && !is_from_proc_macro(cx, expr) |
| 65 | + { |
| 66 | + let default = if fields.len() == 1 { |
| 67 | + // Needs trailing comma to be a single-element tuple |
| 68 | + fields_default[0].to_owned() + "," |
| 69 | + } else { |
| 70 | + fields_default.join(", ") |
| 71 | + }; |
| 72 | + |
| 73 | + span_lint_and_sugg( |
| 74 | + cx, |
| 75 | + TRIVIAL_DEFAULT_CONSTRUCTED_TYPES, |
| 76 | + expr.span, |
| 77 | + &format!( |
| 78 | + "constructing a {} using `default`", |
| 79 | + if fields.is_empty() { "unit" } else { "trivial tuple" }, |
| 80 | + ), |
| 81 | + "try", |
| 82 | + format!("({default})"), |
| 83 | + Applicability::MachineApplicable, |
| 84 | + ); |
| 85 | + } else if let ty::Array(ty, len) = ret_ty.kind() |
| 86 | + && let Some(default) = default_value(cx, *ty) |
| 87 | + && !is_from_proc_macro(cx, expr) |
| 88 | + { |
| 89 | + span_lint_and_sugg( |
| 90 | + cx, |
| 91 | + TRIVIAL_DEFAULT_CONSTRUCTED_TYPES, |
| 92 | + expr.span, |
| 93 | + "constructing a trivial array using `default`", |
| 94 | + "try", |
| 95 | + format!("[{default}; {len}]"), |
| 96 | + Applicability::MachineApplicable, |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/// Gets the default value of `ty`. |
| 104 | +fn default_value(cx: &LateContext<'_>, ty: Ty<'_>) -> Option<&'static str> { |
| 105 | + match ty.kind() { |
| 106 | + ty::Adt(def, _) => { |
| 107 | + if is_lang_item_or_ctor(cx, def.did(), LangItem::Option) { |
| 108 | + return Some("None"); |
| 109 | + } |
| 110 | + |
| 111 | + None |
| 112 | + }, |
| 113 | + ty::Bool => Some("false"), |
| 114 | + ty::Str => Some(r#""""#), |
| 115 | + ty::Int(_) | ty::Uint(_) => Some("0"), |
| 116 | + ty::Float(_) => Some("0.0"), |
| 117 | + // Do not handle `ty::Char`, it's a lot less readable |
| 118 | + _ => None, |
| 119 | + } |
| 120 | +} |
0 commit comments