Skip to content

Commit

Permalink
renamed lint
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ofeeg committed Aug 13, 2023
1 parent 1d77675 commit 0c71347
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 62 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4939,7 +4939,7 @@ Released 2018-09-13
[`iter_skip_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_skip_next
[`iter_with_drain`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_with_drain
[`iterator_step_by_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#iterator_step_by_zero
[`join_absolute_path`]: https://rust-lang.github.io/rust-clippy/master/index.html#join_absolute_path
[`join_absolute_paths`]: https://rust-lang.github.io/rust-clippy/master/index.html#join_absolute_paths
[`just_underscores_and_digits`]: https://rust-lang.github.io/rust-clippy/master/index.html#just_underscores_and_digits
[`large_const_arrays`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_const_arrays
[`large_digit_groups`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_digit_groups
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::methods::ITER_OVEREAGER_CLONED_INFO,
crate::methods::ITER_SKIP_NEXT_INFO,
crate::methods::ITER_WITH_DRAIN_INFO,
crate::methods::JOIN_ABSOLUTE_PATH_INFO,
crate::methods::JOIN_ABSOLUTE_PATHS_INFO,
crate::methods::MANUAL_FILTER_MAP_INFO,
crate::methods::MANUAL_FIND_MAP_INFO,
crate::methods::MANUAL_NEXT_BACK_INFO,
Expand Down
30 changes: 0 additions & 30 deletions clippy_lints/src/methods/join_absolute_path.rs

This file was deleted.

30 changes: 30 additions & 0 deletions clippy_lints/src/methods/join_absolute_paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_ast::ast::LitKind;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext;
use rustc_span::symbol::sym::Path;

use super::JOIN_ABSOLUTE_PATHS;

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>) {
let ty = cx.typeck_results().expr_ty(expr).peel_refs();
if is_type_diagnostic_item(cx, ty, Path)
&& let ExprKind::Lit(spanned) = &join_arg.kind
&& let LitKind::Str(symbol, _) = spanned.node
&& (symbol.as_str().starts_with('/') || symbol.as_str().starts_with('\\'))
{
span_lint_and_then(
cx,
JOIN_ABSOLUTE_PATHS,
join_arg.span,
"argument to `Path::join` starts with a path separator",
|diag| {
diag
.note("joining a path starting with separator will replace the path instead")
.help(r#"if this is unintentional, try removing the starting separator"#)
.help(r#"if this is intentional, try creating a new Path instead"#);
},
);
}
}
46 changes: 25 additions & 21 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod iter_overeager_cloned;
mod iter_skip_next;
mod iter_with_drain;
mod iterator_step_by_zero;
mod join_absolute_path;
mod join_absolute_paths;
mod manual_next_back;
mod manual_ok_or;
mod manual_saturating_arithmetic;
Expand Down Expand Up @@ -2975,8 +2975,8 @@ declare_clippy_lint! {
/// # let mut state = DefaultHasher::new();
/// # let my_enum = Foo::Empty;
/// match my_enum {
/// Empty => ().hash(&mut state),
/// WithValue(x) => x.hash(&mut state),
/// Empty => ().hash(&mut state),
/// WithValue(x) => x.hash(&mut state),
/// }
/// ```
/// Use instead:
Expand All @@ -2988,8 +2988,8 @@ declare_clippy_lint! {
/// # let mut state = DefaultHasher::new();
/// # let my_enum = Foo::Empty;
/// match my_enum {
/// Empty => 0_u8.hash(&mut state),
/// WithValue(x) => x.hash(&mut state),
/// Empty => 0_u8.hash(&mut state),
/// WithValue(x) => x.hash(&mut state),
/// }
/// ```
#[clippy::version = "1.58.0"]
Expand Down Expand Up @@ -3264,37 +3264,39 @@ declare_clippy_lint! {

declare_clippy_lint! {
/// ### What it does
/// Checks for initial `'/ or \\'` in an argument to `.join()` on a `Path`.
/// Checks for calls to `Path::join` that start with a path separator, like `\\` or `/`..
///
/// ### Why is this bad?
/// `.join()` comments starting with a separator (`/` or `\\`) can replace the entire path.
/// If this is intentional, prefer creating a new `Path` instead.
/// `.join()` arguments starting with a separator (`/` or `\\`) can replace the entire path.
/// If this is intentional, prefer using `Path::new()` instead.
///
/// See [`Path::join()`](https://doc.rust-lang.org/std/path/struct.Path.html#method.join)
///
/// ### Example
/// ```rust
/// let path = std::path::Path::new("/bin");
/// let res = path.join("/sh");
/// assert_eq!(res, std::path::PathBuf::from("/sh"));
/// # use std::path::{Path, PathBuf};
/// let path = Path::new("/bin");
/// let joined_path = path.join("/sh");
/// assert_eq!(joined_path, PathBuf::from("/sh"));
/// ```
///
/// Use instead;
/// ```rust
/// let path = std::path::Path::new("/bin");
/// # use std::path::{Path, PathBuf};
/// let path = Path::new("/bin");
///
/// // If this was unintentional, remove the leading separator
/// let extend = path.join("sh");
/// assert_eq!(extend, std::path::PathBuf::from("/bin/sh"));
/// let joined_path = path.join("sh");
/// assert_eq!(joined_path, PathBuf::from("/bin/sh"));
///
/// // If this was intentional, create a new path instead
/// let new = std::path::Path::new("/sh");
/// assert_eq!(new, std::path::PathBuf::from("/sh"));
/// let new = Path::new("/sh");
/// assert_eq!(new, PathBuf::from("/sh"));
/// ```
#[clippy::version = "1.70.0"]
pub JOIN_ABSOLUTE_PATH,
pedantic,
"arg to .join called on a Path contains '/' at the start"
pub JOIN_ABSOLUTE_PATHS,
correctness,
"arg to .join called on a Path contains leading separator"
}

declare_clippy_lint! {
Expand Down Expand Up @@ -3544,7 +3546,7 @@ impl_lint_pass!(Methods => [
NEEDLESS_COLLECT,
SUSPICIOUS_COMMAND_ARG_SPACE,
CLEAR_WITH_DRAIN,
JOIN_ABSOLUTE_PATH,
JOIN_ABSOLUTE_PATHS,
MANUAL_NEXT_BACK,
UNNECESSARY_LITERAL_UNWRAP,
DRAIN_COLLECT,
Expand Down Expand Up @@ -3869,7 +3871,9 @@ impl Methods {
if let Some(("collect", _, _, span, _)) = method_call(recv) {
unnecessary_join::check(cx, expr, recv, join_arg, span);
}
else {join_absolute_path::check(cx, expr, join_arg, span);}
else {
join_absolute_paths::check(cx, recv, join_arg);
}
},
("last", []) | ("skip", [_]) => {
if let Some((name2, recv2, args2, _span2, _)) = method_call(recv) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
//@run-rustfix
#![allow(unused)]
#![warn(clippy::join_absolute_path)]
#![warn(clippy::join_absolute_paths)]
use std::path::Path;

fn main() {
// should be linted
let path = Path::new("/bin");
path.join("/sh");
pathjoin("your/path/here")"/sh");
println!("{}", path.display());

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

// should not be linted
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
//@run-rustfix
#![allow(unused)]
#![warn(clippy::join_absolute_path)]
#![warn(clippy::join_absolute_paths)]
use std::path::Path;

fn main() {
// should be linted
let path = Path::new("/bin");
path.join("/sh");
println!("{}", path.display());

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

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

//should not be linted
let path = Path::new("/bin");
path.join("sh");
println!("{}", path.display());
}
23 changes: 23 additions & 0 deletions tests/ui/join_absolute_paths.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error: argument to `Path::join` starts with a path separator
--> $DIR/join_absolute_paths.rs:8:15
|
LL | path.join("/sh");
| ^^^^^
|
= note: joining a path starting with separator will replace the path instead
= help: if this is unintentional, try removing the starting separator
= help: if this is intentional, try creating a new Path instead
= note: `-D clippy::join-absolute-paths` implied by `-D warnings`

error: argument to `Path::join` starts with a path separator
--> $DIR/join_absolute_paths.rs:12:15
|
LL | path.join("//user");
| ^^^^^^^^
|
= note: joining a path starting with separator will replace the path instead
= help: if this is unintentional, try removing the starting separator
= help: if this is intentional, try creating a new Path instead

error: aborting due to 2 previous errors

0 comments on commit 0c71347

Please sign in to comment.