Skip to content

Commit 5ef1ca1

Browse files
committed
Resolved rest of changes.
1 parent 9f44887 commit 5ef1ca1

File tree

4 files changed

+32
-56
lines changed

4 files changed

+32
-56
lines changed
Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::macros::HirNode;
1+
use clippy_utils::diagnostics::span_lint_and_then;
32
use clippy_utils::ty::is_type_diagnostic_item;
43
use rustc_ast::ast::LitKind;
5-
use rustc_errors::Applicability;
64
use rustc_hir::{Expr, ExprKind};
75
use rustc_lint::LateContext;
86
use rustc_span::symbol::sym::Path;
@@ -11,19 +9,19 @@ use super::JOIN_ABSOLUTE_PATHS;
119

1210
pub(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
}

clippy_lints/src/methods/join_absolute_paths.rs~

Lines changed: 0 additions & 30 deletions
This file was deleted.

clippy_lints/src/methods/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,16 +3267,17 @@ declare_clippy_lint! {
32673267
/// Checks for calls to `Path::join` that start with a path separator, like `\\` or `/`..
32683268
///
32693269
/// ### Why is this bad?
3270-
/// `.join()` comments starting with a separator (`/` or `\\`) can replace the entire path.
3271-
/// If this is intentional, prefer creating a new `Path` instead.
3270+
/// `.join()` arguments starting with a separator (`/` or `\\`) can replace the entire path.
3271+
/// If this is intentional, prefer using `Path::new()` instead.
32723272
///
32733273
/// See [`Path::join()`](https://doc.rust-lang.org/std/path/struct.Path.html#method.join)
32743274
///
32753275
/// ### Example
32763276
/// ```rust
3277-
/// let path = std::path::Path::new("/bin");
3278-
/// let res = path.join("/sh");
3279-
/// assert_eq!(res, std::path::PathBuf::from("/sh"));
3277+
/// # use std::path::{Path, PathBuf};
3278+
/// let path = Path::new("/bin");
3279+
/// let joined_path = path.join("/sh");
3280+
/// assert_eq!(joined_path, PathBuf::from("/sh"));
32803281
/// ```
32813282
///
32823283
/// Use instead;

tests/ui/join_absolute_paths.stderr

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
error: argument to `Path::join` starts with a path separator
2-
--> $DIR/join_absolute_paths.rs:8:9
2+
--> $DIR/join_absolute_paths.rs:8:15
33
|
44
LL | path.join("/sh");
5-
| ^^^^^^ help: try removing first '/' or '/': `join("your/path/here")`
5+
| ^^^^^
66
|
7+
= note: joining a path starting with separator will replace the path instead
8+
= help: if this is unintentional, try removing the starting separator
9+
= help: if this is intentional, try creating a new Path instead
710
= note: `-D clippy::join-absolute-paths` implied by `-D warnings`
811

912
error: argument to `Path::join` starts with a path separator
10-
--> $DIR/join_absolute_paths.rs:13:9
13+
--> $DIR/join_absolute_paths.rs:13:15
1114
|
1215
LL | path.join("//user");
13-
| ^^^^^^ help: try removing first '/' or '/': `join("your/path/here")`
16+
| ^^^^^^^^
17+
|
18+
= note: joining a path starting with separator will replace the path instead
19+
= help: if this is unintentional, try removing the starting separator
20+
= help: if this is intentional, try creating a new Path instead
1421

1522
error: aborting due to 2 previous errors
1623

0 commit comments

Comments
 (0)