Skip to content

Commit 19485a6

Browse files
Lint against keyword lifetimes in keyword_idents
1 parent 9247d0d commit 19485a6

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,8 +1888,16 @@ impl KeywordIdents {
18881888
TokenTree::Token(token, _) => {
18891889
if let Some((ident, token::IdentIsRaw::No)) = token.ident() {
18901890
if !prev_dollar {
1891-
self.check_ident_token(cx, UnderMacro(true), ident);
1891+
self.check_ident_token(cx, UnderMacro(true), ident, "");
18921892
}
1893+
}
1894+
if let Some((ident, token::IdentIsRaw::No)) = token.lifetime() {
1895+
self.check_ident_token(
1896+
cx,
1897+
UnderMacro(true),
1898+
ident.without_first_quote(),
1899+
"'",
1900+
);
18931901
} else if token.kind == TokenKind::Dollar {
18941902
prev_dollar = true;
18951903
continue;
@@ -1906,6 +1914,7 @@ impl KeywordIdents {
19061914
cx: &EarlyContext<'_>,
19071915
UnderMacro(under_macro): UnderMacro,
19081916
ident: Ident,
1917+
prefix: &'static str,
19091918
) {
19101919
let (lint, edition) = match ident.name {
19111920
kw::Async | kw::Await | kw::Try => (KEYWORD_IDENTS_2018, Edition::Edition2018),
@@ -1939,7 +1948,7 @@ impl KeywordIdents {
19391948
cx.emit_span_lint(
19401949
lint,
19411950
ident.span,
1942-
BuiltinKeywordIdents { kw: ident, next: edition, suggestion: ident.span },
1951+
BuiltinKeywordIdents { kw: ident, next: edition, suggestion: ident.span, prefix },
19431952
);
19441953
}
19451954
}
@@ -1952,7 +1961,11 @@ impl EarlyLintPass for KeywordIdents {
19521961
self.check_tokens(cx, &mac.args.tokens);
19531962
}
19541963
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
1955-
self.check_ident_token(cx, UnderMacro(false), ident);
1964+
if ident.name.as_str().starts_with('\'') {
1965+
self.check_ident_token(cx, UnderMacro(false), ident.without_first_quote(), "'");
1966+
} else {
1967+
self.check_ident_token(cx, UnderMacro(false), ident, "");
1968+
}
19561969
}
19571970
}
19581971

compiler/rustc_lint/src/lints.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,9 @@ pub enum BuiltinEllipsisInclusiveRangePatternsLint {
368368
pub struct BuiltinKeywordIdents {
369369
pub kw: Ident,
370370
pub next: Edition,
371-
#[suggestion(code = "r#{kw}", applicability = "machine-applicable")]
371+
#[suggestion(code = "{prefix}r#{kw}", applicability = "machine-applicable")]
372372
pub suggestion: Span,
373+
pub prefix: &'static str,
373374
}
374375

375376
#[derive(LintDiagnostic)]

tests/ui/rust-2024/gen-kw.e2015.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,14 @@ LL | () => { mod test { fn gen() {} } }
3131
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
3232
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
3333

34-
error: aborting due to 3 previous errors
34+
error: `gen` is a keyword in the 2024 edition
35+
--> $DIR/gen-kw.rs:25:9
36+
|
37+
LL | fn test<'gen>() {}
38+
| ^^^^ help: you can use a raw identifier to stay compatible: `'r#gen`
39+
|
40+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
41+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
42+
43+
error: aborting due to 4 previous errors
3544

tests/ui/rust-2024/gen-kw.e2018.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,14 @@ LL | () => { mod test { fn gen() {} } }
3131
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
3232
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
3333

34-
error: aborting due to 3 previous errors
34+
error: `gen` is a keyword in the 2024 edition
35+
--> $DIR/gen-kw.rs:25:9
36+
|
37+
LL | fn test<'gen>() {}
38+
| ^^^^ help: you can use a raw identifier to stay compatible: `'r#gen`
39+
|
40+
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
41+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
42+
43+
error: aborting due to 4 previous errors
3544

tests/ui/rust-2024/gen-kw.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ macro_rules! t {
2222
//[e2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
2323
}
2424

25+
fn test<'gen>() {}
26+
//~^ ERROR `gen` is a keyword in the 2024 edition
27+
//[e2015]~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
28+
//[e2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2024!
29+
2530
t!();

0 commit comments

Comments
 (0)