-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Added new lint "path_join_correction" #10663
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
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9a7bf41
Added new lint for issue #10655
5de0ee9
This commit resolves issue #10655
327cc32
Fixed errors and lack of documentation in mod.rs.
4361151
Added type check and test case for string slice.
25ee587
Added supposed check for windows paths.
c5b5560
Build failure fix.
412dfa6
Build fix attempt #2.
ofeeg e1718e0
Fixed formatting.
ofeeg 1f26d8c
Merge remote-tracking branch 'upstream/master'
ofeeg 08a8a81
Rebase and merge attempt number 2.
ofeeg 606d801
Included sources to std::path in lint string.
ofeeg 048bcc2
Rebase and merge attempt number 2.
ofeeg c36de32
Fixed arguments in lint string.
ofeeg 18bd97f
Renamed lint to join_absolute_path as per request.
ofeeg b694718
Ran cargo dev update_lints
ofeeg b91f9e0
Ran cargo dev bless after a full cargo test
ofeeg 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,30 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, 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::Path, Span}; | ||
|
||
use super::JOIN_ABSOLUTE_PATH; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>, span: Span) { | ||
let ty = cx.typeck_results().expr_ty(expr); | ||
if_chain!( | ||
if is_type_diagnostic_item(cx, ty, Path); | ||
let applicability = Applicability::MachineApplicable; | ||
if let ExprKind::Lit(spanned) = &join_arg.kind; | ||
if let LitKind::Str(symbol, _) = spanned.node; | ||
if symbol.as_str().starts_with('/') || symbol.as_str().starts_with('\\'); | ||
then { | ||
span_lint_and_sugg( | ||
cx, | ||
JOIN_ABSOLUTE_PATH, | ||
span.with_hi(expr.span.hi()), | ||
r#"argument in join called on path contains a starting '/'"#, | ||
"try removing first '/' or '\\'", | ||
"join(\"your/path/here\")".to_owned(), | ||
applicability | ||
); | ||
} | ||
); | ||
} |
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 | ||||
---|---|---|---|---|---|---|
|
@@ -45,6 +45,7 @@ mod iter_overeager_cloned; | |||||
mod iter_skip_next; | ||||||
mod iter_with_drain; | ||||||
mod iterator_step_by_zero; | ||||||
mod join_absolute_path; | ||||||
mod manual_next_back; | ||||||
mod manual_ok_or; | ||||||
mod manual_saturating_arithmetic; | ||||||
|
@@ -3194,6 +3195,41 @@ declare_clippy_lint! { | |||||
"calling `drain` in order to `clear` a container" | ||||||
} | ||||||
|
||||||
declare_clippy_lint! { | ||||||
/// ### What it does | ||||||
/// Checks for initial `'/'` in an argument to `.join()` on a `Path`. | ||||||
/// | ||||||
/// ### Why is this bad? | ||||||
/// `.join()` comments starting with a separator, can replace the entire path. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// If this is intentional, prefer creating a new `Path` 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 instead; | ||||||
/// ```rust | ||||||
/// let path = std::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")); | ||||||
/// | ||||||
/// // If this was intentional, create a new path instead | ||||||
/// let new = std::path::Path::new("/sh"); | ||||||
/// assert_eq!(new, std::path::PathBuf::from("/sh")); | ||||||
/// ``` | ||||||
#[clippy::version = "1.70.0"] | ||||||
pub JOIN_ABSOLUTE_PATH, | ||||||
pedantic, | ||||||
"arg to .join called on a Path contains '/' at the start" | ||||||
} | ||||||
|
||||||
declare_clippy_lint! { | ||||||
/// ### What it does | ||||||
/// Checks for `.rev().next()` on a `DoubleEndedIterator` | ||||||
|
@@ -3345,6 +3381,7 @@ impl_lint_pass!(Methods => [ | |||||
NEEDLESS_COLLECT, | ||||||
SUSPICIOUS_COMMAND_ARG_SPACE, | ||||||
CLEAR_WITH_DRAIN, | ||||||
JOIN_ABSOLUTE_PATH, | ||||||
MANUAL_NEXT_BACK, | ||||||
]); | ||||||
|
||||||
|
@@ -3654,6 +3691,7 @@ 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);} | ||||||
}, | ||||||
("last", []) | ("skip", [_]) => { | ||||||
if let Some((name2, recv2, args2, _span2, _)) = method_call(recv) { | ||||||
|
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,26 @@ | ||||||
// run-rustfix | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
#![allow(unused)] | ||||||
#![warn(clippy::join_absolute_path)] | ||||||
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()); | ||||||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.