1- use clippy_utils:: diagnostics:: span_lint_and_sugg;
2- use clippy_utils:: macros:: HirNode ;
1+ use clippy_utils:: diagnostics:: span_lint_and_then;
32use clippy_utils:: ty:: is_type_diagnostic_item;
43use rustc_ast:: ast:: LitKind ;
5- use rustc_errors:: Applicability ;
64use rustc_hir:: { Expr , ExprKind } ;
75use rustc_lint:: LateContext ;
86use rustc_span:: symbol:: sym:: Path ;
@@ -11,19 +9,19 @@ use super::JOIN_ABSOLUTE_PATHS;
119
1210pub ( super ) fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > , join_arg : & ' tcx Expr < ' tcx > ) {
1311 let ty = cx. typeck_results ( ) . expr_ty ( expr) . peel_refs ( ) ;
14- if !expr. span . from_expansion ( ) && is_type_diagnostic_item ( cx, ty, Path ) {
15- let applicability = Applicability :: MachineApplicable ;
16- if let ExprKind :: Lit ( spanned ) = & join_arg . kind && let LitKind :: Str ( symbol, _ ) = spanned . node
17- && ( symbol . as_str ( ) . starts_with ( '/' ) || symbol . as_str ( ) . starts_with ( '\\' ) ) {
18- span_lint_and_sugg (
19- cx ,
20- JOIN_ABSOLUTE_PATHS ,
21- join_arg . span ( ) . with_hi ( expr . span . hi ( ) ) ,
22- r#"argument to `Path::join` starts with a path separator"# ,
23- "try removing first '/' or ' \\ '" ,
24- "join( \" your/path/here \" )" . to_owned ( ) ,
25- applicability ,
26- ) ;
27- }
12+ if !expr. span . from_expansion ( ) && is_type_diagnostic_item ( cx, ty, Path )
13+ && let ExprKind :: Lit ( spanned ) = & join_arg . kind && let LitKind :: Str ( symbol , _ ) = spanned . node
14+ && ( symbol. as_str ( ) . starts_with ( '/' ) || symbol . as_str ( ) . starts_with ( '\\' ) ) {
15+ span_lint_and_then (
16+ cx ,
17+ JOIN_ABSOLUTE_PATHS ,
18+ join_arg . span ,
19+ "argument to `Path::join` starts with a path separator" ,
20+ |diag| {
21+ diag . note ( "joining a path starting with separator will replace the path instead" ) ;
22+ diag . help ( r#"if this is unintentional, try removing the starting separator"# ) ;
23+ diag . help ( r#"if this is intentional, try creating a new Path instead"# ) ;
24+ } ,
25+ ) ;
2826 } ;
2927}
0 commit comments