2024-07-22 13:32:27 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2024-04-16 18:23:30 -05:00
|
|
|
use rustc_ast::ast::{BindingMode, Pat, PatKind};
|
2023-05-20 08:39:26 -05:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::declare_lint_pass;
|
2023-05-20 08:39:26 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Checks for usages of the `ref` keyword.
|
For restriction lints, replace “Why is this bad?” with “Why restrict this?”
The `restriction` group contains many lints which are not about
necessarily “bad” things, but style choices — perhaps even style choices
which contradict conventional Rust style — or are otherwise very
situational. This results in silly wording like “Why is this bad?
It isn't, but ...”, which I’ve seen confuse a newcomer at least once.
To improve this situation, this commit replaces the “Why is this bad?”
section heading with “Why restrict this?”, for most, but not all,
restriction lints. I left alone the ones whose placement in the
restriction group is more incidental.
In order to make this make sense, I had to remove the “It isn't, but”
texts from the contents of the sections. Sometimes further changes
were needed, or there were obvious fixes to make, and I went ahead
and made those changes without attempting to split them into another
commit, even though many of them are not strictly necessary for the
“Why restrict this?” project.
2024-05-23 00:21:01 -05:00
|
|
|
///
|
|
|
|
/// ### Why restrict this?
|
2023-05-20 08:39:26 -05:00
|
|
|
/// The `ref` keyword can be confusing for people unfamiliar with it, and often
|
|
|
|
/// it is more concise to use `&` instead.
|
For restriction lints, replace “Why is this bad?” with “Why restrict this?”
The `restriction` group contains many lints which are not about
necessarily “bad” things, but style choices — perhaps even style choices
which contradict conventional Rust style — or are otherwise very
situational. This results in silly wording like “Why is this bad?
It isn't, but ...”, which I’ve seen confuse a newcomer at least once.
To improve this situation, this commit replaces the “Why is this bad?”
section heading with “Why restrict this?”, for most, but not all,
restriction lints. I left alone the ones whose placement in the
restriction group is more incidental.
In order to make this make sense, I had to remove the “It isn't, but”
texts from the contents of the sections. Sometimes further changes
were needed, or there were obvious fixes to make, and I went ahead
and made those changes without attempting to split them into another
commit, even though many of them are not strictly necessary for the
“Why restrict this?” project.
2024-05-23 00:21:01 -05:00
|
|
|
///
|
2023-05-20 08:39:26 -05:00
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2023-05-20 08:39:26 -05:00
|
|
|
/// let opt = Some(5);
|
|
|
|
/// if let Some(ref foo) = opt {}
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2023-05-20 08:39:26 -05:00
|
|
|
/// let opt = Some(5);
|
|
|
|
/// if let Some(foo) = &opt {}
|
|
|
|
/// ```
|
|
|
|
#[clippy::version = "1.71.0"]
|
|
|
|
pub REF_PATTERNS,
|
|
|
|
restriction,
|
|
|
|
"use of a ref pattern, e.g. Some(ref value)"
|
|
|
|
}
|
|
|
|
declare_lint_pass!(RefPatterns => [REF_PATTERNS]);
|
|
|
|
|
|
|
|
impl EarlyLintPass for RefPatterns {
|
|
|
|
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) {
|
2024-04-16 18:23:30 -05:00
|
|
|
if let PatKind::Ident(BindingMode::REF, _, _) = pat.kind
|
2023-11-02 11:35:56 -05:00
|
|
|
&& !pat.span.from_expansion()
|
2023-05-20 08:39:26 -05:00
|
|
|
{
|
2024-07-22 13:32:27 -05:00
|
|
|
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
|
|
|
|
span_lint_and_then(cx, REF_PATTERNS, pat.span, "usage of ref pattern", |diag| {
|
|
|
|
diag.help("consider using `&` for clarity instead");
|
|
|
|
});
|
2023-05-20 08:39:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|