Skip to content

Commit ff8ac58

Browse files
committed
new lint: clippy::ref_pattern
1 parent 3aab0dd commit ff8ac58

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4978,6 +4978,7 @@ Released 2018-09-13
49784978
[`ref_binding_to_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_binding_to_reference
49794979
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
49804980
[`ref_option_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_option_ref
4981+
[`ref_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_pattern
49814982
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro
49824983
[`repeat_once`]: https://rust-lang.github.io/rust-clippy/master/index.html#repeat_once
49834984
[`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
539539
crate::redundant_slicing::REDUNDANT_SLICING_INFO,
540540
crate::redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES_INFO,
541541
crate::ref_option_ref::REF_OPTION_REF_INFO,
542+
crate::ref_pattern::REF_PATTERN_INFO,
542543
crate::reference::DEREF_ADDROF_INFO,
543544
crate::regex::INVALID_REGEX_INFO,
544545
crate::regex::TRIVIAL_REGEX_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ mod redundant_pub_crate;
266266
mod redundant_slicing;
267267
mod redundant_static_lifetimes;
268268
mod ref_option_ref;
269+
mod ref_pattern;
269270
mod reference;
270271
mod regex;
271272
mod return_self_not_must_use;
@@ -972,6 +973,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
972973
store.register_early_pass(|| Box::new(suspicious_doc_comments::SuspiciousDocComments));
973974
store.register_late_pass(|_| Box::new(items_after_test_module::ItemsAfterTestModule));
974975
store.register_late_pass(|_| Box::new(default_constructed_unit_structs::DefaultConstructedUnitStructs));
976+
store.register_early_pass(|| Box::new(ref_pattern::RefPattern));
975977
// add lints here, do not remove this comment, it's used in `new_lint`
976978
}
977979

clippy_lints/src/ref_pattern.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_ast::ast::*;
3+
use rustc_lint::{EarlyContext, EarlyLintPass};
4+
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
6+
declare_clippy_lint! {
7+
/// ### What it does
8+
/// Checks for usages of the `ref` keyword.
9+
/// ### Why is this bad?
10+
/// The `ref` keyword can be confusing for people unfamiliar with it, and often
11+
/// it is more concise to use `&` instead.
12+
/// ### Example
13+
/// ```rust
14+
/// let opt = Some(5);
15+
/// if let Some(ref foo) = opt {}
16+
/// ```
17+
/// Use instead:
18+
/// ```rust
19+
/// let opt = Some(5);
20+
/// if let Some(foo) = &opt {}
21+
/// ```
22+
#[clippy::version = "1.71.0"]
23+
pub REF_PATTERN,
24+
restriction,
25+
"use of a ref pattern, e.g. Some(ref value)"
26+
}
27+
declare_lint_pass!(RefPattern => [REF_PATTERN]);
28+
29+
impl EarlyLintPass for RefPattern {
30+
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) {
31+
match pat.kind {
32+
PatKind::Ident(BindingAnnotation::REF, _, _) => {
33+
span_lint_and_help(
34+
cx,
35+
REF_PATTERN,
36+
pat.span,
37+
"usage of ref pattern",
38+
None,
39+
"consider using `&` for clarity instead.",
40+
);
41+
},
42+
_ => {},
43+
}
44+
}
45+
}

tests/ui/ref_pattern.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![allow(unused)]
2+
#![allow(clippy::toplevel_ref_arg)]
3+
#![warn(clippy::ref_pattern)]
4+
5+
fn use_in_pattern() {
6+
let opt = Some(5);
7+
match opt {
8+
None => {},
9+
Some(ref opt) => {},
10+
}
11+
}
12+
13+
fn use_in_binding() {
14+
let x = 5;
15+
let ref y = x;
16+
}
17+
18+
fn main() {}

tests/ui/ref_pattern.stderr

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: usage of ref pattern
2+
--> $DIR/ref_pattern.rs:9:14
3+
|
4+
LL | Some(ref opt) => {},
5+
| ^^^^^^^
6+
|
7+
= help: consider using `&` for clarity instead.
8+
= note: `-D clippy::ref-pattern` implied by `-D warnings`
9+
10+
error: usage of ref pattern
11+
--> $DIR/ref_pattern.rs:15:9
12+
|
13+
LL | let ref y = x;
14+
| ^^^^^
15+
|
16+
= help: consider using `&` for clarity instead.
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)