|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use rustc_ast::ast::LitKind; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::intravisit::{FnKind,Visitor}; |
| 5 | +use rustc_hir::{Body,Expr,ExprKind,FnDecl,FnRetTy,Lit,MutTy,Mutability,Ty,TyKind, intravisit}; |
| 6 | +use rustc_lint::{LateContext,LateLintPass}; |
| 7 | +use rustc_session::declare_lint_pass; |
| 8 | +use rustc_span::Span; |
| 9 | +use rustc_span::def_id::LocalDefId; |
| 10 | + |
| 11 | +declare_clippy_lint!{ |
| 12 | +/// ### What it does |
| 13 | +/// |
| 14 | +/// Detects functions that are written to return `&str` that could return `&'static str` but instead return a `&'a str`. |
| 15 | +/// |
| 16 | +/// ### Why is this bad? |
| 17 | +/// |
| 18 | +/// This leaves the caller unable to use the `&str` as `&'static str`, causing unneccessary allocations or confusion. |
| 19 | +/// This is also most likely what you meant to write. |
| 20 | +/// |
| 21 | +/// ### Example |
| 22 | +/// ```no_run |
| 23 | +/// # struct MyType; |
| 24 | +/// impl MyType { |
| 25 | +/// fn returns_literal(&self) -> &str { |
| 26 | +/// "Literal" |
| 27 | +/// } |
| 28 | +/// } |
| 29 | +/// ``` |
| 30 | +/// Use instead: |
| 31 | +/// ```no_run |
| 32 | +/// # struct MyType; |
| 33 | +/// impl MyType { |
| 34 | +/// fn returns_literal(&self) -> &'static str { |
| 35 | +/// "Literal" |
| 36 | +/// } |
| 37 | +/// } |
| 38 | +/// ``` |
| 39 | +/// Or, in case you may return a non-literal `str` in future: |
| 40 | +/// ```no_run |
| 41 | +/// # struct MyType; |
| 42 | +/// impl MyType { |
| 43 | +/// fn returns_literal<'a>(&'a self) -> &'a str { |
| 44 | +/// "Literal" |
| 45 | +/// } |
| 46 | +/// } |
| 47 | +/// ``` |
| 48 | + #[clippy::version = "1.83.0"] |
| 49 | +pubUNNECESSARY_LITERAL_BOUND, |
| 50 | + pedantic, |
| 51 | +"detects &str that could be &'static str in function return types" |
| 52 | +} |
| 53 | + |
| 54 | +declare_lint_pass!(UnnecessaryLiteralBound => [UNNECESSARY_LITERAL_BOUND]); |
| 55 | + |
| 56 | +fnextract_anonymous_ref<'tcx>(hir_ty:&Ty<'tcx>) -> Option<&'tcxTy<'tcx>>{ |
| 57 | +letTyKind::Ref(lifetime,MutTy{ ty, mutbl }) = hir_ty.kindelse{ |
| 58 | +returnNone; |
| 59 | +}; |
| 60 | + |
| 61 | +if !lifetime.is_anonymous() || !matches!(mutbl,Mutability::Not){ |
| 62 | +returnNone; |
| 63 | +} |
| 64 | + |
| 65 | +Some(ty) |
| 66 | +} |
| 67 | + |
| 68 | +fnis_str_literal(expr:&Expr<'_>) -> bool{ |
| 69 | +matches!( |
| 70 | + expr.kind, |
| 71 | +ExprKind::Lit(Lit{ |
| 72 | + node:LitKind::Str(..), |
| 73 | + .. |
| 74 | +}), |
| 75 | +) |
| 76 | +} |
| 77 | + |
| 78 | +structFindNonLiteralReturn; |
| 79 | + |
| 80 | +impl<'hir>Visitor<'hir>forFindNonLiteralReturn{ |
| 81 | +typeResult = std::ops::ControlFlow<()>; |
| 82 | +typeNestedFilter = intravisit::nested_filter::None; |
| 83 | + |
| 84 | +fnvisit_expr(&mutself,expr:&'hirExpr<'hir>) -> Self::Result{ |
| 85 | +ifletExprKind::Ret(Some(ret_val_expr)) = expr.kind |
| 86 | + && !is_str_literal(ret_val_expr) |
| 87 | +{ |
| 88 | +Self::Result::Break(()) |
| 89 | +}else{ |
| 90 | + intravisit::walk_expr(self, expr) |
| 91 | +} |
| 92 | +} |
| 93 | +} |
| 94 | + |
| 95 | +fncheck_implicit_returns_static_str(body:&Body<'_>) -> bool{ |
| 96 | +// TODO: Improve this to the same complexity as the Visitor to catch more implicit return cases. |
| 97 | +ifletExprKind::Block(block, _) = body.value.kind |
| 98 | + && letSome(implicit_ret) = block.expr |
| 99 | +{ |
| 100 | +returnis_str_literal(implicit_ret); |
| 101 | +} |
| 102 | + |
| 103 | +false |
| 104 | +} |
| 105 | + |
| 106 | +fncheck_explicit_returns_static_str(expr:&Expr<'_>) -> bool{ |
| 107 | +letmut visitor = FindNonLiteralReturn; |
| 108 | + visitor.visit_expr(expr).is_continue() |
| 109 | +} |
| 110 | + |
| 111 | +impl<'tcx>LateLintPass<'tcx>forUnnecessaryLiteralBound{ |
| 112 | +fncheck_fn( |
| 113 | +&mutself, |
| 114 | +cx:&LateContext<'tcx>, |
| 115 | +kind:FnKind<'tcx>, |
| 116 | +decl:&'tcxFnDecl<'_>, |
| 117 | +body:&'tcxBody<'_>, |
| 118 | +span:Span, |
| 119 | + _:LocalDefId, |
| 120 | +){ |
| 121 | +if span.from_expansion(){ |
| 122 | +return; |
| 123 | +} |
| 124 | + |
| 125 | +// Checking closures would be a little silly |
| 126 | +ifmatches!(kind,FnKind::Closure){ |
| 127 | +return; |
| 128 | +} |
| 129 | + |
| 130 | +// Check for `-> &str` |
| 131 | +letFnRetTy::Return(ret_hir_ty) = decl.outputelse{ |
| 132 | +return; |
| 133 | +}; |
| 134 | + |
| 135 | +letSome(inner_hir_ty) = extract_anonymous_ref(ret_hir_ty)else{ |
| 136 | +return; |
| 137 | +}; |
| 138 | + |
| 139 | +if !rustc_hir_analysis::lower_ty(cx.tcx, inner_hir_ty).is_str(){ |
| 140 | +return; |
| 141 | +} |
| 142 | + |
| 143 | +// Check for all return statements returning literals |
| 144 | +ifcheck_explicit_returns_static_str(body.value) && check_implicit_returns_static_str(body){ |
| 145 | +span_lint_and_sugg( |
| 146 | + cx, |
| 147 | +UNNECESSARY_LITERAL_BOUND, |
| 148 | + ret_hir_ty.span, |
| 149 | +"returning a `str` unnecessarily tied to the lifetime of arguments", |
| 150 | +"try", |
| 151 | +"&'static str".into(),// how ironic, a lint about `&'static str` requiring a `String` alloc... |
| 152 | +Applicability::MachineApplicable, |
| 153 | +); |
| 154 | +} |
| 155 | +} |
| 156 | +} |
0 commit comments