-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint clippy::join_absolute_paths
#11453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::expr_or_init; | ||
use clippy_utils::source::snippet_opt; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_ast::ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym; | ||
use rustc_span::Span; | ||
|
||
use super::JOIN_ABSOLUTE_PATHS; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, recv: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>, expr_span: Span) { | ||
let ty = cx.typeck_results().expr_ty(recv).peel_refs(); | ||
if (is_type_diagnostic_item(cx, ty, sym::Path) || is_type_diagnostic_item(cx, ty, sym::PathBuf)) | ||
&& let ExprKind::Lit(spanned) = expr_or_init(cx, join_arg).kind | ||
&& let LitKind::Str(symbol, _) = spanned.node | ||
&& let sym_str = symbol.as_str() | ||
&& sym_str.starts_with(['/', '\\']) | ||
{ | ||
span_lint_and_then( | ||
cx, | ||
JOIN_ABSOLUTE_PATHS, | ||
join_arg.span, | ||
"argument to `Path::join` starts with a path separator", | ||
|diag| { | ||
let arg_str = snippet_opt(cx, spanned.span).unwrap_or_else(|| "..".to_string()); | ||
|
||
let no_separator = if sym_str.starts_with('/') { | ||
arg_str.replacen('/', "", 1) | ||
} else { | ||
arg_str.replacen('\\', "", 1) | ||
}; | ||
|
||
diag.note("joining a path starting with separator will replace the path instead") | ||
.span_suggestion( | ||
spanned.span, | ||
"if this is unintentional, try removing the starting separator", | ||
no_separator, | ||
Applicability::Unspecified, | ||
) | ||
.span_suggestion( | ||
expr_span, | ||
"if this is intentional, try using `Path::new` instead", | ||
format!("PathBuf::from({arg_str})"), | ||
Applicability::Unspecified, | ||
); | ||
}, | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//@no-rustfix | ||
|
||
#![allow(clippy::needless_raw_string_hashes)] | ||
#![warn(clippy::join_absolute_paths)] | ||
|
||
use std::path::{Path, PathBuf}; | ||
|
||
fn main() { | ||
let path = Path::new("/bin"); | ||
path.join("/sh"); | ||
xFrednet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//~^ ERROR: argument to `Path::join` starts with a path separator | ||
|
||
let path = Path::new("C:\\Users"); | ||
path.join("\\user"); | ||
//~^ ERROR: argument to `Path::join` starts with a path separator | ||
|
||
let path = PathBuf::from("/bin"); | ||
path.join("/sh"); | ||
//~^ ERROR: argument to `Path::join` starts with a path separator | ||
|
||
let path = PathBuf::from("/bin"); | ||
path.join(r#"/sh"#); | ||
//~^ ERROR: argument to `Path::join` starts with a path separator | ||
|
||
let path: &[&str] = &["/bin"]; | ||
path.join("/sh"); | ||
|
||
let path = Path::new("/bin"); | ||
path.join("sh"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
error: argument to `Path::join` starts with a path separator | ||
--> $DIR/join_absolute_paths.rs:10:15 | ||
| | ||
LL | path.join("/sh"); | ||
| ^^^^^ | ||
| | ||
= note: joining a path starting with separator will replace the path instead | ||
= note: `-D clippy::join-absolute-paths` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::join_absolute_paths)]` | ||
help: if this is unintentional, try removing the starting separator | ||
| | ||
LL | path.join("sh"); | ||
| ~~~~ | ||
help: if this is intentional, try using `Path::new` instead | ||
| | ||
LL | PathBuf::from("/sh"); | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: argument to `Path::join` starts with a path separator | ||
--> $DIR/join_absolute_paths.rs:14: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 | ||
| | ||
LL | path.join("\user"); | ||
| ~~~~~~~ | ||
help: if this is intentional, try using `Path::new` instead | ||
| | ||
LL | PathBuf::from("\\user"); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: argument to `Path::join` starts with a path separator | ||
--> $DIR/join_absolute_paths.rs:18: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 | ||
| | ||
LL | path.join("sh"); | ||
| ~~~~ | ||
help: if this is intentional, try using `Path::new` instead | ||
| | ||
LL | PathBuf::from("/sh"); | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: argument to `Path::join` starts with a path separator | ||
--> $DIR/join_absolute_paths.rs:22:15 | ||
| | ||
LL | path.join(r#"/sh"#); | ||
| ^^^^^^^^ | ||
| | ||
= note: joining a path starting with separator will replace the path instead | ||
help: if this is unintentional, try removing the starting separator | ||
| | ||
LL | path.join(r#"sh"#); | ||
| ~~~~~~~ | ||
help: if this is intentional, try using `Path::new` instead | ||
| | ||
LL | PathBuf::from(r#"/sh"#); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to 4 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.