@@ -3218,19 +3218,32 @@ declare_clippy_lint! {
3218
3218
}
3219
3219
3220
3220
declare_clippy_lint ! {
3221
- /// ### What it does
3222
- /// Checks for initial '/' in an argument to .join on a path.
3221
+ /// Checks for initial `'/'` in an argument to `.join()` on a `Path`.
3222
+ ///
3223
3223
/// ### Why is this bad?
3224
- /// .join() calls on paths already attach the '/'.
3224
+ /// `.join()` comments starting with a separator, can replace the entire path.
3225
+ /// If this is intentional, prefer creating a new `Path` instead.
3226
+ ///
3227
+ /// See [`Path::join()`](https://doc.rust-lang.org/std/path/struct.Path.html#method.join)
3228
+ ///
3225
3229
/// ### Example
3226
3230
/// ```rust
3227
- /// // example code where clippy issues a warning
3228
3231
/// let path = std::path::Path::new("/bin");
3229
- /// path.join("/sh");
3232
+ /// let res = path.join("/sh");
3233
+ /// assert_eq!(res, PathBuf::from("/sh"));
3230
3234
/// ```
3235
+ ///
3231
3236
/// Use instead:
3232
3237
/// ```rust
3233
- /// // path.join("sh");
3238
+ /// let path = std::path::Path::new("/bin");
3239
+ ///
3240
+ /// // If this was unintentional, remove the leading separator
3241
+ /// let extend = path.join("sh");
3242
+ /// assert_eq!(extend, PathBuf::from("/bin/sh"));
3243
+ ///
3244
+ /// // If this was intentional, create a new path instead
3245
+ /// let new = Path::new("/sh")
3246
+ /// assert_eq!(new PathBuf::from("/sh"));
3234
3247
/// ```
3235
3248
#[ clippy:: version = "1.70.0" ]
3236
3249
pub PATH_JOIN_CORRECTION ,
@@ -3678,7 +3691,7 @@ impl Methods {
3678
3691
if let Some ( ( "collect" , _, _, span, _) ) = method_call ( recv) {
3679
3692
unnecessary_join:: check ( cx, expr, recv, join_arg, span) ;
3680
3693
}
3681
- else { path_join_correction:: check ( cx, expr, join_arg, span) ; }
3694
+ else { path_join_correction:: check ( cx, expr, join_arg, span) ; }
3682
3695
} ,
3683
3696
( "last" , [ ] ) | ( "skip" , [ _] ) => {
3684
3697
if let Some ( ( name2, recv2, args2, _span2, _) ) = method_call ( recv) {
0 commit comments