@@ -1884,30 +1884,33 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnameableTestFunctions {
18841884}
18851885
18861886declare_lint ! {
1887- pub ASYNC_IDENTS ,
1887+ pub KEYWORD_IDENTS ,
18881888 Allow ,
1889- "detects `async` being used as an identifier"
1889+ "detects edition keywords being used as an identifier"
18901890}
18911891
1892- /// Checks for uses of `async` as an identifier
1892+ /// Checks for uses of edtion keywords used as an identifier
18931893#[ derive( Clone ) ]
1894- pub struct Async2018 ;
1894+ pub struct KeywordIdents ;
18951895
1896- impl LintPass for Async2018 {
1896+ impl LintPass for KeywordIdents {
18971897 fn get_lints ( & self ) -> LintArray {
1898- lint_array ! ( ASYNC_IDENTS )
1898+ lint_array ! ( KEYWORD_IDENTS )
18991899 }
19001900}
19011901
1902- impl Async2018 {
1902+ impl KeywordIdents {
19031903 fn check_tokens ( & mut self , cx : & EarlyContext , tokens : TokenStream ) {
19041904 for tt in tokens. into_trees ( ) {
19051905 match tt {
19061906 TokenTree :: Token ( span, tok) => match tok. ident ( ) {
19071907 // only report non-raw idents
1908- Some ( ( ident, false ) ) if ident. as_str ( ) == "async" => {
1909- self . report ( cx, span. substitute_dummy ( ident. span ) )
1910- } ,
1908+ Some ( ( ident, false ) ) => {
1909+ self . check_ident ( cx, ast:: Ident {
1910+ span : span. substitute_dummy ( ident. span ) ,
1911+ ..ident
1912+ } ) ;
1913+ }
19111914 _ => { } ,
19121915 }
19131916 TokenTree :: Delimited ( _, ref delim) => {
@@ -1916,38 +1919,47 @@ impl Async2018 {
19161919 }
19171920 }
19181921 }
1919- fn report ( & mut self , cx : & EarlyContext , span : Span ) {
1920- // don't lint `r#async`
1921- if cx. sess . parse_sess . raw_identifier_spans . borrow ( ) . contains ( & span) {
1922- return ;
1923- }
1924- let mut lint = cx. struct_span_lint (
1925- ASYNC_IDENTS ,
1926- span,
1927- "`async` is a keyword in the 2018 edition" ,
1928- ) ;
1929-
1930- // Don't suggest about raw identifiers if the feature isn't active
1931- lint. span_suggestion_with_applicability (
1932- span,
1933- "you can use a raw identifier to stay compatible" ,
1934- "r#async" . to_string ( ) ,
1935- Applicability :: MachineApplicable ,
1936- ) ;
1937- lint. emit ( )
1938- }
19391922}
19401923
1941- impl EarlyLintPass for Async2018 {
1924+ impl EarlyLintPass for KeywordIdents {
19421925 fn check_mac_def ( & mut self , cx : & EarlyContext , mac_def : & ast:: MacroDef , _id : ast:: NodeId ) {
19431926 self . check_tokens ( cx, mac_def. stream ( ) ) ;
19441927 }
19451928 fn check_mac ( & mut self , cx : & EarlyContext , mac : & ast:: Mac ) {
19461929 self . check_tokens ( cx, mac. node . tts . clone ( ) . into ( ) ) ;
19471930 }
19481931 fn check_ident ( & mut self , cx : & EarlyContext , ident : ast:: Ident ) {
1949- if ident. as_str ( ) == "async" {
1950- self . report ( cx, ident. span ) ;
1932+ let next_edition = match cx. sess . edition ( ) {
1933+ Edition :: Edition2015 => {
1934+ match & ident. as_str ( ) [ ..] {
1935+ "async" |
1936+ "try" => Edition :: Edition2018 ,
1937+ _ => return ,
1938+ }
1939+ }
1940+
1941+ // no new keywords yet for 2018 edition and beyond
1942+ _ => return ,
1943+ } ;
1944+
1945+ // don't lint `r#foo`
1946+ if cx. sess . parse_sess . raw_identifier_spans . borrow ( ) . contains ( & ident. span ) {
1947+ return ;
19511948 }
1949+
1950+ let mut lint = cx. struct_span_lint (
1951+ KEYWORD_IDENTS ,
1952+ ident. span ,
1953+ & format ! ( "`{}` is a keyword in the {} edition" ,
1954+ ident. as_str( ) ,
1955+ next_edition) ,
1956+ ) ;
1957+ lint. span_suggestion_with_applicability (
1958+ ident. span ,
1959+ "you can use a raw identifier to stay compatible" ,
1960+ format ! ( "r#{}" , ident. as_str( ) ) ,
1961+ Applicability :: MachineApplicable ,
1962+ ) ;
1963+ lint. emit ( )
19521964 }
19531965}
0 commit comments