Skip to content

Commit 0c71347

Browse files
committed
renamed lint
Removed if chain. Added the rest of the suggested changes. Applied cargo test supposed changes. Resolved rest of changes. Resolved nitpicks. Update clippy_lints/src/methods/join_absolute_paths.rs Co-authored-by: Catherine Flores <[email protected]> Formatted and blessed. Got rid of println! calls.
1 parent 1d77675 commit 0c71347

8 files changed

+84
-62
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4939,7 +4939,7 @@ Released 2018-09-13
49394939
[`iter_skip_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_skip_next
49404940
[`iter_with_drain`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_with_drain
49414941
[`iterator_step_by_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#iterator_step_by_zero
4942-
[`join_absolute_path`]: https://rust-lang.github.io/rust-clippy/master/index.html#join_absolute_path
4942+
[`join_absolute_paths`]: https://rust-lang.github.io/rust-clippy/master/index.html#join_absolute_paths
49434943
[`just_underscores_and_digits`]: https://rust-lang.github.io/rust-clippy/master/index.html#just_underscores_and_digits
49444944
[`large_const_arrays`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_const_arrays
49454945
[`large_digit_groups`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_digit_groups

clippy_lints/src/declared_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
359359
crate::methods::ITER_OVEREAGER_CLONED_INFO,
360360
crate::methods::ITER_SKIP_NEXT_INFO,
361361
crate::methods::ITER_WITH_DRAIN_INFO,
362-
crate::methods::JOIN_ABSOLUTE_PATH_INFO,
362+
crate::methods::JOIN_ABSOLUTE_PATHS_INFO,
363363
crate::methods::MANUAL_FILTER_MAP_INFO,
364364
crate::methods::MANUAL_FIND_MAP_INFO,
365365
crate::methods::MANUAL_NEXT_BACK_INFO,

clippy_lints/src/methods/join_absolute_path.rs

-30
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::ty::is_type_diagnostic_item;
3+
use rustc_ast::ast::LitKind;
4+
use rustc_hir::{Expr, ExprKind};
5+
use rustc_lint::LateContext;
6+
use rustc_span::symbol::sym::Path;
7+
8+
use super::JOIN_ABSOLUTE_PATHS;
9+
10+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>) {
11+
let ty = cx.typeck_results().expr_ty(expr).peel_refs();
12+
if is_type_diagnostic_item(cx, ty, Path)
13+
&& let ExprKind::Lit(spanned) = &join_arg.kind
14+
&& let LitKind::Str(symbol, _) = spanned.node
15+
&& (symbol.as_str().starts_with('/') || symbol.as_str().starts_with('\\'))
16+
{
17+
span_lint_and_then(
18+
cx,
19+
JOIN_ABSOLUTE_PATHS,
20+
join_arg.span,
21+
"argument to `Path::join` starts with a path separator",
22+
|diag| {
23+
diag
24+
.note("joining a path starting with separator will replace the path instead")
25+
.help(r#"if this is unintentional, try removing the starting separator"#)
26+
.help(r#"if this is intentional, try creating a new Path instead"#);
27+
},
28+
);
29+
}
30+
}

clippy_lints/src/methods/mod.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mod iter_overeager_cloned;
4646
mod iter_skip_next;
4747
mod iter_with_drain;
4848
mod iterator_step_by_zero;
49-
mod join_absolute_path;
49+
mod join_absolute_paths;
5050
mod manual_next_back;
5151
mod manual_ok_or;
5252
mod manual_saturating_arithmetic;
@@ -2975,8 +2975,8 @@ declare_clippy_lint! {
29752975
/// # let mut state = DefaultHasher::new();
29762976
/// # let my_enum = Foo::Empty;
29772977
/// match my_enum {
2978-
/// Empty => ().hash(&mut state),
2979-
/// WithValue(x) => x.hash(&mut state),
2978+
/// Empty => ().hash(&mut state),
2979+
/// WithValue(x) => x.hash(&mut state),
29802980
/// }
29812981
/// ```
29822982
/// Use instead:
@@ -2988,8 +2988,8 @@ declare_clippy_lint! {
29882988
/// # let mut state = DefaultHasher::new();
29892989
/// # let my_enum = Foo::Empty;
29902990
/// match my_enum {
2991-
/// Empty => 0_u8.hash(&mut state),
2992-
/// WithValue(x) => x.hash(&mut state),
2991+
/// Empty => 0_u8.hash(&mut state),
2992+
/// WithValue(x) => x.hash(&mut state),
29932993
/// }
29942994
/// ```
29952995
#[clippy::version = "1.58.0"]
@@ -3264,37 +3264,39 @@ declare_clippy_lint! {
32643264

32653265
declare_clippy_lint! {
32663266
/// ### What it does
3267-
/// Checks for initial `'/ or \\'` in an argument to `.join()` on a `Path`.
3267+
/// 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;
32833284
/// ```rust
3284-
/// let path = std::path::Path::new("/bin");
3285+
/// # use std::path::{Path, PathBuf};
3286+
/// let path = Path::new("/bin");
32853287
///
32863288
/// // If this was unintentional, remove the leading separator
3287-
/// let extend = path.join("sh");
3288-
/// assert_eq!(extend, std::path::PathBuf::from("/bin/sh"));
3289+
/// let joined_path = path.join("sh");
3290+
/// assert_eq!(joined_path, PathBuf::from("/bin/sh"));
32893291
///
32903292
/// // If this was intentional, create a new path instead
3291-
/// let new = std::path::Path::new("/sh");
3292-
/// assert_eq!(new, std::path::PathBuf::from("/sh"));
3293+
/// let new = Path::new("/sh");
3294+
/// assert_eq!(new, PathBuf::from("/sh"));
32933295
/// ```
32943296
#[clippy::version = "1.70.0"]
3295-
pub JOIN_ABSOLUTE_PATH,
3296-
pedantic,
3297-
"arg to .join called on a Path contains '/' at the start"
3297+
pub JOIN_ABSOLUTE_PATHS,
3298+
correctness,
3299+
"arg to .join called on a Path contains leading separator"
32983300
}
32993301

33003302
declare_clippy_lint! {
@@ -3544,7 +3546,7 @@ impl_lint_pass!(Methods => [
35443546
NEEDLESS_COLLECT,
35453547
SUSPICIOUS_COMMAND_ARG_SPACE,
35463548
CLEAR_WITH_DRAIN,
3547-
JOIN_ABSOLUTE_PATH,
3549+
JOIN_ABSOLUTE_PATHS,
35483550
MANUAL_NEXT_BACK,
35493551
UNNECESSARY_LITERAL_UNWRAP,
35503552
DRAIN_COLLECT,
@@ -3869,7 +3871,9 @@ impl Methods {
38693871
if let Some(("collect", _, _, span, _)) = method_call(recv) {
38703872
unnecessary_join::check(cx, expr, recv, join_arg, span);
38713873
}
3872-
else {join_absolute_path::check(cx, expr, join_arg, span);}
3874+
else {
3875+
join_absolute_paths::check(cx, recv, join_arg);
3876+
}
38733877
},
38743878
("last", []) | ("skip", [_]) => {
38753879
if let Some((name2, recv2, args2, _span2, _)) = method_call(recv) {

tests/ui/join_absolute_path.fixed renamed to tests/ui/join_absolute_paths.fixed

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
//@run-rustfix
22
#![allow(unused)]
3-
#![warn(clippy::join_absolute_path)]
3+
#![warn(clippy::join_absolute_paths)]
44
use std::path::Path;
55

66
fn main() {
77
// should be linted
88
let path = Path::new("/bin");
9-
path.join("/sh");
9+
pathjoin("your/path/here")"/sh");
1010
println!("{}", path.display());
1111

1212
//should be linted
1313
let path = Path::new("C:\\Users");
14-
path.join("\\user");
14+
pathjoin("your/path/here")"\\user");
1515
println!("{}", path.display());
1616

1717
// should not be linted
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
//@run-rustfix
21
#![allow(unused)]
3-
#![warn(clippy::join_absolute_path)]
2+
#![warn(clippy::join_absolute_paths)]
43
use std::path::Path;
54

65
fn main() {
76
// should be linted
87
let path = Path::new("/bin");
98
path.join("/sh");
10-
println!("{}", path.display());
119

1210
//should be linted
1311
let path = Path::new("C:\\Users");
1412
path.join("\\user");
15-
println!("{}", path.display());
1613

1714
// should not be linted
1815
let path: &[&str] = &["/bin"];
1916
path.join("/sh");
20-
println!("{:?}", path);
2117

2218
//should not be linted
2319
let path = Path::new("/bin");
2420
path.join("sh");
25-
println!("{}", path.display());
2621
}

tests/ui/join_absolute_paths.stderr

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: argument to `Path::join` starts with a path separator
2+
--> $DIR/join_absolute_paths.rs:8:15
3+
|
4+
LL | path.join("/sh");
5+
| ^^^^^
6+
|
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
10+
= note: `-D clippy::join-absolute-paths` implied by `-D warnings`
11+
12+
error: argument to `Path::join` starts with a path separator
13+
--> $DIR/join_absolute_paths.rs:12:15
14+
|
15+
LL | path.join("//user");
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
21+
22+
error: aborting due to 2 previous errors
23+

0 commit comments

Comments
 (0)