Skip to content

Commit 4bca5f9

Browse files
committed
fix: do not emit clippy::toplevel_ref_arg if clippy::ref_pattern is enabled
1 parent 7c82656 commit 4bca5f9

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

clippy_lints/src/misc.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ use rustc_span::source_map::{ExpnKind, Span};
1616

1717
use clippy_utils::sugg::Sugg;
1818
use clippy_utils::{
19-
get_parent_expr, in_constant, is_integer_literal, is_no_std_crate, iter_input_pats, last_path_segment, SpanlessEq,
19+
get_parent_expr, in_constant, is_integer_literal, is_lint_allowed, is_no_std_crate, iter_input_pats,
20+
last_path_segment, SpanlessEq,
2021
};
2122

23+
use crate::ref_pattern::REF_PATTERN;
24+
2225
declare_clippy_lint! {
2326
/// ### What it does
2427
/// Checks for function arguments and let bindings denoted as
@@ -162,6 +165,10 @@ impl<'tcx> LateLintPass<'tcx> for LintPass {
162165
return;
163166
}
164167
for arg in iter_input_pats(decl, body) {
168+
// Do not emit if clippy::ref_pattern is not allowed to avoid having two lints for the same issue.
169+
if !is_lint_allowed(cx, REF_PATTERN, arg.pat.hir_id) {
170+
return;
171+
}
165172
if let PatKind::Binding(BindingAnnotation(ByRef::Yes, _), ..) = arg.pat.kind {
166173
span_lint(
167174
cx,
@@ -180,6 +187,8 @@ impl<'tcx> LateLintPass<'tcx> for LintPass {
180187
if let StmtKind::Local(local) = stmt.kind;
181188
if let PatKind::Binding(BindingAnnotation(ByRef::Yes, mutabl), .., name, None) = local.pat.kind;
182189
if let Some(init) = local.init;
190+
// Do not emit if clippy::ref_pattern is not allowed to avoid having two lints for the same issue.
191+
if is_lint_allowed(cx, REF_PATTERN, local.pat.hir_id);
183192
then {
184193
let ctxt = local.span.ctxt();
185194
let mut app = Applicability::MachineApplicable;
@@ -220,6 +229,8 @@ impl<'tcx> LateLintPass<'tcx> for LintPass {
220229
if let ExprKind::Binary(ref binop, a, b) = expr.kind;
221230
if binop.node == BinOpKind::And || binop.node == BinOpKind::Or;
222231
if let Some(sugg) = Sugg::hir_opt(cx, a);
232+
// Do not emit if clippy::ref_pattern is not allowed to avoid having two lints for the same issue.
233+
if is_lint_allowed(cx, REF_PATTERN, expr.hir_id);
223234
then {
224235
span_lint_hir_and_then(
225236
cx,
@@ -252,6 +263,10 @@ impl<'tcx> LateLintPass<'tcx> for LintPass {
252263
// Don't lint things expanded by #[derive(...)], etc or `await` desugaring
253264
return;
254265
}
266+
// Do not emit if clippy::ref_pattern is not allowed to avoid having two lints for the same issue.
267+
if !is_lint_allowed(cx, REF_PATTERN, expr.hir_id) {
268+
return;
269+
}
255270
let sym;
256271
let binding = match expr.kind {
257272
ExprKind::Path(ref qpath) if !matches!(qpath, hir::QPath::LangItem(..)) => {

tests/ui/ref_pattern.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![allow(unused)]
2-
#![allow(clippy::toplevel_ref_arg)]
32
#![warn(clippy::ref_pattern)]
43

54
fn use_in_pattern() {
@@ -15,4 +14,6 @@ fn use_in_binding() {
1514
let ref y = x;
1615
}
1716

17+
fn use_in_parameter(ref x: i32) {}
18+
1819
fn main() {}

tests/ui/ref_pattern.stderr

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: usage of ref pattern
2-
--> $DIR/ref_pattern.rs:9:14
2+
--> $DIR/ref_pattern.rs:8:14
33
|
44
LL | Some(ref opt) => {},
55
| ^^^^^^^
@@ -8,12 +8,20 @@ LL | Some(ref opt) => {},
88
= note: `-D clippy::ref-pattern` implied by `-D warnings`
99

1010
error: usage of ref pattern
11-
--> $DIR/ref_pattern.rs:15:9
11+
--> $DIR/ref_pattern.rs:14:9
1212
|
1313
LL | let ref y = x;
1414
| ^^^^^
1515
|
1616
= help: consider using `&` for clarity instead
1717

18-
error: aborting due to 2 previous errors
18+
error: usage of ref pattern
19+
--> $DIR/ref_pattern.rs:17:21
20+
|
21+
LL | fn use_in_parameter(ref x: i32) {}
22+
| ^^^^^
23+
|
24+
= help: consider using `&` for clarity instead
25+
26+
error: aborting due to 3 previous errors
1927

0 commit comments

Comments
 (0)