|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use rustc_ast::LitKind; |
| 3 | +use rustc_errors::Applicability::MachineApplicable; |
| 4 | +use rustc_hir::{Expr, ExprKind, PathSegment, QPath, TyKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_middle::ty; |
| 7 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 8 | +use rustc_span::{sym, symbol, Span}; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// |
| 13 | + /// Checks for usage of `""` to create a `String`, such as `"".to_string()`, `"".to_owned()`, |
| 14 | + /// `String::from("")` and others. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// |
| 18 | + /// Different ways of creating an empty string makes your code less standardized, which can |
| 19 | + /// be confusing. |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```rust |
| 23 | + /// let a = "".to_string(); |
| 24 | + /// let b: String = "".into(); |
| 25 | + /// ``` |
| 26 | + /// Use instead: |
| 27 | + /// ```rust |
| 28 | + /// let a = String::new(); |
| 29 | + /// let b = String::new(); |
| 30 | + /// ``` |
| 31 | + #[clippy::version = "1.65.0"] |
| 32 | + pub MANUAL_EMPTY_STRING_CREATIONS, |
| 33 | + style, |
| 34 | + "empty String is being created manually" |
| 35 | +} |
| 36 | +declare_lint_pass!(ManualEmptyStringCreations => [MANUAL_EMPTY_STRING_CREATIONS]); |
| 37 | + |
| 38 | +impl LateLintPass<'_> for ManualEmptyStringCreations { |
| 39 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 40 | + if expr.span.from_expansion() { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + let ty = cx.typeck_results().expr_ty(expr); |
| 45 | + match ty.kind() { |
| 46 | + ty::Adt(adt_def, _) if adt_def.is_struct() => { |
| 47 | + if !cx.tcx.is_diagnostic_item(sym::String, adt_def.did()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + }, |
| 51 | + _ => return, |
| 52 | + } |
| 53 | + |
| 54 | + match expr.kind { |
| 55 | + ExprKind::Call(func, args) => { |
| 56 | + parse_call(cx, expr.span, func, args); |
| 57 | + }, |
| 58 | + ExprKind::MethodCall(path_segment, args, _) => { |
| 59 | + parse_method_call(cx, expr.span, path_segment, args); |
| 60 | + }, |
| 61 | + _ => (), |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// Checks if an expression's kind corresponds to an empty &str. |
| 67 | +fn is_expr_kind_empty_str(expr_kind: &ExprKind<'_>) -> bool { |
| 68 | + if let ExprKind::Lit(lit) = expr_kind && |
| 69 | + let LitKind::Str(value, _) = lit.node && |
| 70 | + value == symbol::kw::Empty |
| 71 | + { |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + false |
| 76 | +} |
| 77 | + |
| 78 | +/// Emits the `MANUAL_EMPTY_STRING_CREATION` warning and suggests the appropriate fix. |
| 79 | +fn warn_then_suggest(cx: &LateContext<'_>, span: Span) { |
| 80 | + span_lint_and_sugg( |
| 81 | + cx, |
| 82 | + MANUAL_EMPTY_STRING_CREATIONS, |
| 83 | + span, |
| 84 | + "empty String is being created manually", |
| 85 | + "consider using", |
| 86 | + "String::new()".into(), |
| 87 | + MachineApplicable, |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +/// Tries to parse an expression as a method call, emiting the warning if necessary. |
| 92 | +fn parse_method_call(cx: &LateContext<'_>, span: Span, path_segment: &PathSegment<'_>, args: &[Expr<'_>]) { |
| 93 | + if args.is_empty() { |
| 94 | + // When parsing TryFrom::try_from(...).expect(...), we will have more than 1 arg. |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + let ident = path_segment.ident.as_str(); |
| 99 | + let method_arg_kind = &args[0].kind; |
| 100 | + if ["to_string", "to_owned", "into"].contains(&ident) && is_expr_kind_empty_str(method_arg_kind) { |
| 101 | + warn_then_suggest(cx, span); |
| 102 | + } else if let ExprKind::Call(func, args) = method_arg_kind { |
| 103 | + // If our first argument is a function call itself, it could be an `unwrap`-like function. |
| 104 | + // E.g. String::try_from("hello").unwrap(), TryFrom::try_from("").expect("hello"), etc. |
| 105 | + parse_call(cx, span, func, args); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// Tries to parse an expression as a function call, emiting the warning if necessary. |
| 110 | +fn parse_call(cx: &LateContext<'_>, span: Span, func: &Expr<'_>, args: &[Expr<'_>]) { |
| 111 | + if args.len() != 1 { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + let arg_kind = &args[0].kind; |
| 116 | + if let ExprKind::Path(qpath) = &func.kind { |
| 117 | + if let QPath::TypeRelative(_, _) = qpath { |
| 118 | + // String::from(...) or String::try_from(...) |
| 119 | + if let QPath::TypeRelative(ty, path_seg) = qpath && |
| 120 | + [sym::from, sym::try_from].contains(&path_seg.ident.name) && |
| 121 | + let TyKind::Path(qpath) = &ty.kind && |
| 122 | + let QPath::Resolved(_, path) = qpath && |
| 123 | + let [path_seg] = path.segments && |
| 124 | + path_seg.ident.name == sym::String && |
| 125 | + is_expr_kind_empty_str(arg_kind) |
| 126 | + { |
| 127 | + warn_then_suggest(cx, span); |
| 128 | + } |
| 129 | + } else if let QPath::Resolved(_, path) = qpath { |
| 130 | + // From::from(...) or TryFrom::try_from(...) |
| 131 | + if let [path_seg1, path_seg2] = path.segments && |
| 132 | + is_expr_kind_empty_str(arg_kind) && ( |
| 133 | + (path_seg1.ident.name == sym::From && path_seg2.ident.name == sym::from) || |
| 134 | + (path_seg1.ident.name == sym::TryFrom && path_seg2.ident.name == sym::try_from) |
| 135 | + ) |
| 136 | + { |
| 137 | + warn_then_suggest(cx, span); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments