Skip to content

Commit 5054e8c

Browse files
Lint against keyword lifetimes in keyword_idents
1 parent afa24f0 commit 5054e8c

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

compiler/rustc_lint/src/builtin.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1851,9 +1851,16 @@ impl KeywordIdents {
18511851
TokenTree::Token(token, _) => {
18521852
if let Some((ident, token::IdentIsRaw::No)) = token.ident() {
18531853
if !prev_dollar {
1854-
self.check_ident_token(cx, UnderMacro(true), ident);
1854+
self.check_ident_token(cx, UnderMacro(true), ident, "");
18551855
}
1856-
} else if *token == TokenKind::Dollar {
1856+
} else if let Some((ident, token::IdentIsRaw::No)) = token.lifetime() {
1857+
self.check_ident_token(
1858+
cx,
1859+
UnderMacro(true),
1860+
ident.without_first_quote(),
1861+
"'",
1862+
);
1863+
} else if token.kind == TokenKind::Dollar {
18571864
prev_dollar = true;
18581865
continue;
18591866
}
@@ -1869,6 +1876,7 @@ impl KeywordIdents {
18691876
cx: &EarlyContext<'_>,
18701877
UnderMacro(under_macro): UnderMacro,
18711878
ident: Ident,
1879+
prefix: &'static str,
18721880
) {
18731881
let (lint, edition) = match ident.name {
18741882
kw::Async | kw::Await | kw::Try => (KEYWORD_IDENTS_2018, Edition::Edition2018),
@@ -1902,7 +1910,7 @@ impl KeywordIdents {
19021910
cx.emit_span_lint(
19031911
lint,
19041912
ident.span,
1905-
BuiltinKeywordIdents { kw: ident, next: edition, suggestion: ident.span },
1913+
BuiltinKeywordIdents { kw: ident, next: edition, suggestion: ident.span, prefix },
19061914
);
19071915
}
19081916
}
@@ -1915,7 +1923,11 @@ impl EarlyLintPass for KeywordIdents {
19151923
self.check_tokens(cx, &mac.args.tokens);
19161924
}
19171925
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
1918-
self.check_ident_token(cx, UnderMacro(false), ident);
1926+
if ident.name.as_str().starts_with('\'') {
1927+
self.check_ident_token(cx, UnderMacro(false), ident.without_first_quote(), "'");
1928+
} else {
1929+
self.check_ident_token(cx, UnderMacro(false), ident, "");
1930+
}
19191931
}
19201932
}
19211933

compiler/rustc_lint/src/lints.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,9 @@ pub(crate) enum BuiltinEllipsisInclusiveRangePatternsLint {
363363
pub(crate) struct BuiltinKeywordIdents {
364364
pub kw: Ident,
365365
pub next: Edition,
366-
#[suggestion(code = "r#{kw}", applicability = "machine-applicable")]
366+
#[suggestion(code = "{prefix}r#{kw}", applicability = "machine-applicable")]
367367
pub suggestion: Span,
368+
pub prefix: &'static str,
368369
}
369370

370371
#[derive(LintDiagnostic)]

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

+10-1
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

+10-1
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

+5
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)