|
1 | | -use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir, span_lint_hir_and_then}; |
2 | | -use clippy_utils::source::{snippet, snippet_with_context}; |
| 1 | +use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then}; |
3 | 2 | use clippy_utils::sugg::Sugg; |
4 | | -use clippy_utils::{ |
5 | | - SpanlessEq, fulfill_or_allowed, get_parent_expr, in_automatically_derived, is_lint_allowed, iter_input_pats, |
6 | | - last_path_segment, |
7 | | -}; |
| 3 | +use clippy_utils::{SpanlessEq, fulfill_or_allowed, get_parent_expr, in_automatically_derived, last_path_segment}; |
8 | 4 | use rustc_errors::Applicability; |
9 | 5 | use rustc_hir::def::Res; |
10 | | -use rustc_hir::intravisit::FnKind; |
11 | | -use rustc_hir::{ |
12 | | - BinOpKind, BindingMode, Body, ByRef, Expr, ExprKind, FnDecl, Mutability, PatKind, QPath, Stmt, StmtKind, |
13 | | -}; |
| 6 | +use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, Stmt, StmtKind}; |
14 | 7 | use rustc_lint::{LateContext, LateLintPass, LintContext}; |
15 | 8 | use rustc_session::declare_lint_pass; |
16 | | -use rustc_span::Span; |
17 | | -use rustc_span::def_id::LocalDefId; |
18 | | - |
19 | | -use crate::ref_patterns::REF_PATTERNS; |
20 | | - |
21 | | -declare_clippy_lint! { |
22 | | - /// ### What it does |
23 | | - /// Checks for function arguments and let bindings denoted as |
24 | | - /// `ref`. |
25 | | - /// |
26 | | - /// ### Why is this bad? |
27 | | - /// The `ref` declaration makes the function take an owned |
28 | | - /// value, but turns the argument into a reference (which means that the value |
29 | | - /// is destroyed when exiting the function). This adds not much value: either |
30 | | - /// take a reference type, or take an owned value and create references in the |
31 | | - /// body. |
32 | | - /// |
33 | | - /// For let bindings, `let x = &foo;` is preferred over `let ref x = foo`. The |
34 | | - /// type of `x` is more obvious with the former. |
35 | | - /// |
36 | | - /// ### Known problems |
37 | | - /// If the argument is dereferenced within the function, |
38 | | - /// removing the `ref` will lead to errors. This can be fixed by removing the |
39 | | - /// dereferences, e.g., changing `*x` to `x` within the function. |
40 | | - /// |
41 | | - /// ### Example |
42 | | - /// ```no_run |
43 | | - /// fn foo(ref _x: u8) {} |
44 | | - /// ``` |
45 | | - /// |
46 | | - /// Use instead: |
47 | | - /// ```no_run |
48 | | - /// fn foo(_x: &u8) {} |
49 | | - /// ``` |
50 | | - #[clippy::version = "pre 1.29.0"] |
51 | | - pub TOPLEVEL_REF_ARG, |
52 | | - style, |
53 | | - "an entire binding declared as `ref`, in a function argument or a `let` statement" |
54 | | -} |
55 | 9 |
|
56 | 10 | declare_clippy_lint! { |
57 | 11 | /// ### What it does |
@@ -140,79 +94,13 @@ declare_clippy_lint! { |
140 | 94 | } |
141 | 95 |
|
142 | 96 | declare_lint_pass!(LintPass => [ |
143 | | - TOPLEVEL_REF_ARG, |
144 | 97 | USED_UNDERSCORE_BINDING, |
145 | 98 | USED_UNDERSCORE_ITEMS, |
146 | 99 | SHORT_CIRCUIT_STATEMENT, |
147 | 100 | ]); |
148 | 101 |
|
149 | 102 | impl<'tcx> LateLintPass<'tcx> for LintPass { |
150 | | - fn check_fn( |
151 | | - &mut self, |
152 | | - cx: &LateContext<'tcx>, |
153 | | - k: FnKind<'tcx>, |
154 | | - decl: &'tcx FnDecl<'_>, |
155 | | - body: &'tcx Body<'_>, |
156 | | - _: Span, |
157 | | - _: LocalDefId, |
158 | | - ) { |
159 | | - if !matches!(k, FnKind::Closure) { |
160 | | - for arg in iter_input_pats(decl, body) { |
161 | | - if let PatKind::Binding(BindingMode(ByRef::Yes(_), _), ..) = arg.pat.kind |
162 | | - && is_lint_allowed(cx, REF_PATTERNS, arg.pat.hir_id) |
163 | | - && !arg.span.in_external_macro(cx.tcx.sess.source_map()) |
164 | | - { |
165 | | - span_lint_hir( |
166 | | - cx, |
167 | | - TOPLEVEL_REF_ARG, |
168 | | - arg.hir_id, |
169 | | - arg.pat.span, |
170 | | - "`ref` directly on a function parameter does not prevent taking ownership of the passed argument. \ |
171 | | - Consider using a reference type instead", |
172 | | - ); |
173 | | - } |
174 | | - } |
175 | | - } |
176 | | - } |
177 | | - |
178 | 103 | fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) { |
179 | | - if let StmtKind::Let(local) = stmt.kind |
180 | | - && let PatKind::Binding(BindingMode(ByRef::Yes(mutabl), _), .., name, None) = local.pat.kind |
181 | | - && let Some(init) = local.init |
182 | | - // Do not emit if clippy::ref_patterns is not allowed to avoid having two lints for the same issue. |
183 | | - && is_lint_allowed(cx, REF_PATTERNS, local.pat.hir_id) |
184 | | - && !stmt.span.in_external_macro(cx.tcx.sess.source_map()) |
185 | | - { |
186 | | - let ctxt = local.span.ctxt(); |
187 | | - let mut app = Applicability::MachineApplicable; |
188 | | - let sugg_init = Sugg::hir_with_context(cx, init, ctxt, "..", &mut app); |
189 | | - let (mutopt, initref) = if mutabl == Mutability::Mut { |
190 | | - ("mut ", sugg_init.mut_addr()) |
191 | | - } else { |
192 | | - ("", sugg_init.addr()) |
193 | | - }; |
194 | | - let tyopt = if let Some(ty) = local.ty { |
195 | | - let ty_snip = snippet_with_context(cx, ty.span, ctxt, "_", &mut app).0; |
196 | | - format!(": &{mutopt}{ty_snip}") |
197 | | - } else { |
198 | | - String::new() |
199 | | - }; |
200 | | - span_lint_hir_and_then( |
201 | | - cx, |
202 | | - TOPLEVEL_REF_ARG, |
203 | | - init.hir_id, |
204 | | - local.pat.span, |
205 | | - "`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead", |
206 | | - |diag| { |
207 | | - diag.span_suggestion( |
208 | | - stmt.span, |
209 | | - "try", |
210 | | - format!("let {name}{tyopt} = {initref};", name = snippet(cx, name.span, ".."),), |
211 | | - app, |
212 | | - ); |
213 | | - }, |
214 | | - ); |
215 | | - } |
216 | 104 | if let StmtKind::Semi(expr) = stmt.kind |
217 | 105 | && let ExprKind::Binary(binop, a, b) = &expr.kind |
218 | 106 | && matches!(binop.node, BinOpKind::And | BinOpKind::Or) |
|
0 commit comments