-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[squash-merged] new lint: single_char_lifetime_names
#8236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
PatchMixolydic
wants to merge
2
commits into
rust-lang:master
from
PatchMixolydic:single_char_lifetime_names
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_ast::ast::{GenericParam, GenericParamKind}; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for lifetimes with names which are one character | ||
/// long. | ||
/// | ||
/// ### Why is this bad? | ||
/// A single character is likely not enough to express the | ||
/// purpose of a lifetime. Using a longer name can make code | ||
/// easier to understand, especially for those who are new to | ||
/// Rust. | ||
/// | ||
/// ### Known problems | ||
/// Rust programmers and learning resources tend to use single | ||
/// character lifetimes, so this lint is at odds with the | ||
/// ecosystem at large. In addition, the lifetime's purpose may | ||
/// be obvious or, rarely, expressible in one character. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// struct DiagnosticCtx<'a> { | ||
/// source: &'a str, | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// struct DiagnosticCtx<'src> { | ||
/// source: &'src str, | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.59.0"] | ||
pub SINGLE_CHAR_LIFETIME_NAMES, | ||
restriction, | ||
"warns against single-character lifetime names" | ||
} | ||
|
||
declare_lint_pass!(SingleCharLifetimeNames => [SINGLE_CHAR_LIFETIME_NAMES]); | ||
|
||
impl EarlyLintPass for SingleCharLifetimeNames { | ||
fn check_generic_param(&mut self, ctx: &EarlyContext<'_>, param: &GenericParam) { | ||
if in_external_macro(ctx.sess, param.ident.span) { | ||
return; | ||
} | ||
|
||
if let GenericParamKind::Lifetime = param.kind { | ||
if !param.is_placeholder && param.ident.as_str().len() <= 2 { | ||
span_lint_and_help( | ||
ctx, | ||
SINGLE_CHAR_LIFETIME_NAMES, | ||
param.ident.span, | ||
"single-character lifetime names are likely uninformative", | ||
None, | ||
"use a more informative name", | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#![warn(clippy::single_char_lifetime_names)] | ||
|
||
// Lifetimes should only be linted when they're introduced | ||
struct DiagnosticCtx<'a, 'b> | ||
where | ||
'a: 'b, | ||
{ | ||
_source: &'a str, | ||
_unit: &'b (), | ||
} | ||
|
||
// Only the lifetimes on the `impl`'s generics should be linted | ||
impl<'a, 'b> DiagnosticCtx<'a, 'b> { | ||
fn new(source: &'a str, unit: &'b ()) -> DiagnosticCtx<'a, 'b> { | ||
Self { | ||
_source: source, | ||
_unit: unit, | ||
} | ||
} | ||
} | ||
|
||
// No lifetimes should be linted here | ||
impl<'src, 'unit> DiagnosticCtx<'src, 'unit> { | ||
fn new_pass(source: &'src str, unit: &'unit ()) -> DiagnosticCtx<'src, 'unit> { | ||
Self { | ||
_source: source, | ||
_unit: unit, | ||
} | ||
} | ||
} | ||
|
||
// Only 'a should be linted here | ||
fn split_once<'a>(base: &'a str, other: &'_ str) -> (&'a str, Option<&'a str>) { | ||
base.split_once(other) | ||
.map(|(left, right)| (left, Some(right))) | ||
.unwrap_or((base, None)) | ||
} | ||
|
||
fn main() { | ||
let src = "loop {}"; | ||
let unit = (); | ||
DiagnosticCtx::new(src, &unit); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
error: single-character lifetime names are likely uninformative | ||
--> $DIR/single_char_lifetime_names.rs:4:22 | ||
| | ||
LL | struct DiagnosticCtx<'a, 'b> | ||
| ^^ | ||
| | ||
= note: `-D clippy::single-char-lifetime-names` implied by `-D warnings` | ||
= help: use a more informative name | ||
|
||
error: single-character lifetime names are likely uninformative | ||
--> $DIR/single_char_lifetime_names.rs:4:26 | ||
| | ||
LL | struct DiagnosticCtx<'a, 'b> | ||
| ^^ | ||
| | ||
= help: use a more informative name | ||
|
||
error: single-character lifetime names are likely uninformative | ||
--> $DIR/single_char_lifetime_names.rs:13:6 | ||
| | ||
LL | impl<'a, 'b> DiagnosticCtx<'a, 'b> { | ||
| ^^ | ||
| | ||
= help: use a more informative name | ||
|
||
error: single-character lifetime names are likely uninformative | ||
--> $DIR/single_char_lifetime_names.rs:13:10 | ||
| | ||
LL | impl<'a, 'b> DiagnosticCtx<'a, 'b> { | ||
| ^^ | ||
| | ||
= help: use a more informative name | ||
|
||
error: single-character lifetime names are likely uninformative | ||
--> $DIR/single_char_lifetime_names.rs:33:15 | ||
| | ||
LL | fn split_once<'a>(base: &'a str, other: &'_ str) -> (&'a str, Option<&'a str>) { | ||
| ^^ | ||
| | ||
= help: use a more informative name | ||
|
||
error: aborting due to 5 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.