@@ -2,14 +2,15 @@ use clippy_utils::{
22 diagnostics:: span_lint_and_then, get_parent_expr, is_from_proc_macro, match_def_path, path_res, paths:: PATH_NEW ,
33 ty:: is_type_diagnostic_item,
44} ;
5- use rustc_ast:: LitKind ;
5+ use rustc_ast:: { LitKind , StrStyle } ;
66use rustc_errors:: Applicability ;
77use rustc_hir:: def_id:: DefId ;
88use rustc_hir:: { Expr , ExprKind , QPath } ;
99use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
1010use rustc_middle:: { lint:: in_external_macro, ty} ;
1111use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1212use rustc_span:: { sym, Symbol } ;
13+ use std:: borrow:: Cow ;
1314
1415declare_clippy_lint ! {
1516 /// ### What it does
@@ -41,7 +42,7 @@ impl<'tcx> LateLintPass<'tcx> for BareDosDeviceNames {
4142 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
4243 if !in_external_macro ( cx. sess ( ) , expr. span )
4344 && let ExprKind :: Lit ( arg) = expr. kind
44- && let LitKind :: Str ( str_sym, _ ) = arg. node
45+ && let LitKind :: Str ( str_sym, str_style ) = arg. node
4546 && matches ! (
4647 & * str_sym. as_str( ) . to_ascii_lowercase( ) ,
4748 "aux"
@@ -86,20 +87,26 @@ impl<'tcx> LateLintPass<'tcx> for BareDosDeviceNames {
8687 expr. span ,
8788 "this path refers to a DOS device" ,
8889 |diag| {
90+ // Keep `r###` and `###`
91+ let ( prefix, hashes) = if let StrStyle :: Raw ( num) = str_style {
92+ ( Cow :: Borrowed ( "r" ) , "#" . repeat ( num as usize ) . into ( ) )
93+ } else {
94+ ( Cow :: Borrowed ( "" ) , Cow :: Borrowed ( "" ) )
95+ } ;
96+
8997 // Suggest making current behavior explicit
9098 diag. span_suggestion_verbose (
9199 expr. span ,
92- "if this is intended, try" ,
93- // FIXME: I have zero clue why it normalizes this. `\` -> `/`
94- format ! ( r#"r"\\.\{str_sym}"\"# ) ,
100+ "if this is intended, use" ,
101+ format ! ( r#"r{hashes}"\\.\{str_sym}"{hashes}"# ) ,
95102 Applicability :: MaybeIncorrect ,
96103 ) ;
97104
98105 // Suggest making the code refer to a file or folder in the current directory
99106 diag. span_suggestion_verbose (
100107 expr. span ,
101- "if this was intended to point to a file or folder, try " ,
102- format ! ( " \ " ./{str_sym}\" " ) ,
108+ "if this was intended to point to a file or folder, use " ,
109+ format ! ( r#"{prefix}{hashes} "./{str_sym}"{hashes}"# ) ,
103110 Applicability :: MaybeIncorrect ,
104111 ) ;
105112 }
0 commit comments