Skip to content

Commit e1718e0

Browse files
committed
Fixed formatting.
1 parent 412dfa6 commit e1718e0

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3218,19 +3218,32 @@ declare_clippy_lint! {
32183218
}
32193219

32203220
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+
///
32233223
/// ### 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+
///
32253229
/// ### Example
32263230
/// ```rust
3227-
/// // example code where clippy issues a warning
32283231
/// let path = std::path::Path::new("/bin");
3229-
/// path.join("/sh");
3232+
/// let res = path.join("/sh");
3233+
/// assert_eq!(res, PathBuf::from("/sh"));
32303234
/// ```
3235+
///
32313236
/// Use instead:
32323237
/// ```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"));
32343247
/// ```
32353248
#[clippy::version = "1.70.0"]
32363249
pub PATH_JOIN_CORRECTION,
@@ -3678,7 +3691,7 @@ impl Methods {
36783691
if let Some(("collect", _, _, span, _)) = method_call(recv) {
36793692
unnecessary_join::check(cx, expr, recv, join_arg, span);
36803693
}
3681-
else {path_join_correction::check(cx, expr, join_arg, span);}
3694+
else {path_join_correction::check(cx, expr, join_arg, span);}
36823695
},
36833696
("last", []) | ("skip", [_]) => {
36843697
if let Some((name2, recv2, args2, _span2, _)) = method_call(recv) {

clippy_lints/src/methods/path_join_correction.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ use super::PATH_JOIN_CORRECTION;
1010
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>, span: Span) {
1111
let ty = cx.typeck_results().expr_ty(expr);
1212
if_chain!(
13-
if is_type_diagnostic_item(cx, ty, Path);
14-
let applicability = Applicability::MachineApplicable;
15-
if let ExprKind::Lit(spanned) = &join_arg.kind;
16-
if let LitKind::Str(symbol, _) = spanned.node;
17-
if symbol.as_str().starts_with('/') || symbol.as_str().starts_with('\\');
13+
if is_type_diagnostic_item(cx, ty, Path);
14+
let applicability = Applicability::MachineApplicable;
15+
if let ExprKind::Lit(spanned) = &join_arg.kind;
16+
if let LitKind::Str(symbol, _) = spanned.node;
17+
if symbol.as_str().starts_with('/') || symbol.as_str().starts_with('\\');
1818
then {
1919
span_lint_and_sugg(
2020
cx,
2121
PATH_JOIN_CORRECTION,
2222
span.with_hi(expr.span.hi()),
23-
r#"argument in join called on path contains a starting '/'"#,
24-
"try removing first '/' or '\\'",
25-
"join(\"your/path/here\")".to_owned(),
26-
applicability
27-
);
28-
}
29-
);
23+
r#"argument in join called on path contains a starting '/'"#,
24+
"try removing first '/' or '\\'",
25+
"join(\"your/path/here\")".to_owned(),
26+
applicability
27+
);
28+
}
29+
);
3030
}

0 commit comments

Comments
 (0)