Skip to content

Commit

Permalink
Added new lint for issue rust-lang#10655
Browse files Browse the repository at this point in the history
  • Loading branch information
Mason Ray Hanna III committed Apr 17, 2023
1 parent 90cb0fa commit 9a7bf41
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4814,6 +4814,7 @@ Released 2018-09-13
[`partialeq_ne_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_ne_impl
[`partialeq_to_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_to_none
[`path_buf_push_overwrite`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_buf_push_overwrite
[`path_join_correction`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_join_correction
[`pattern_type_mismatch`]: https://rust-lang.github.io/rust-clippy/master/index.html#pattern_type_mismatch
[`permissions_set_readonly_false`]: https://rust-lang.github.io/rust-clippy/master/index.html#permissions_set_readonly_false
[`positional_named_format_parameters`]: https://rust-lang.github.io/rust-clippy/master/index.html#positional_named_format_parameters
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::methods::OR_FUN_CALL_INFO,
crate::methods::OR_THEN_UNWRAP_INFO,
crate::methods::PATH_BUF_PUSH_OVERWRITE_INFO,
crate::methods::PATH_JOIN_CORRECTION_INFO,
crate::methods::RANGE_ZIP_WITH_LEN_INFO,
crate::methods::REPEAT_ONCE_INFO,
crate::methods::RESULT_MAP_OR_INTO_OPTION_INFO,
Expand Down
25 changes: 25 additions & 0 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod path_join_correction;
mod bind_instead_of_map;
mod bytecount;
mod bytes_count_to_len;
Expand Down Expand Up @@ -3216,6 +3217,25 @@ declare_clippy_lint! {
"calling `drain` in order to `clear` a container"
}

declare_clippy_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Example
/// ```rust
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```rust
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "1.70.0"]
pub PATH_JOIN_CORRECTION,
pedantic,
"default lint description"
}

pub struct Methods {
avoid_breaking_exported_api: bool,
msrv: Msrv,
Expand Down Expand Up @@ -3345,6 +3365,7 @@ impl_lint_pass!(Methods => [
NEEDLESS_COLLECT,
SUSPICIOUS_COMMAND_ARG_SPACE,
CLEAR_WITH_DRAIN,
PATH_JOIN_CORRECTION,
]);

/// Extracts a method call name, args, and `Span` of the method name.
Expand Down Expand Up @@ -3655,7 +3676,11 @@ impl Methods {
if let Some(("collect", _, _, span, _)) = method_call(recv) {
unnecessary_join::check(cx, expr, recv, join_arg, span);
}
else {path_join_correction::check(cx, expr, join_arg, span);}
},
/*("join", [join_arg]) => {
path_join_correction::check(cx, expr, join_arg, span);
},*/
("last", []) | ("skip", [_]) => {
if let Some((name2, recv2, args2, _span2, _)) = method_call(recv) {
if let ("cloned", []) = (name2, args2) {
Expand Down
35 changes: 35 additions & 0 deletions clippy_lints/src/methods/path_join_correction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use rustc_lint::{LateContext};
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_span::Span;

use super::PATH_JOIN_CORRECTION;

// TODO: Adjust the parameters as necessary

pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
expr: &'tcx Expr<'tcx>,
join_arg: &'tcx Expr<'tcx>,
span: Span,
) {
let applicability = Applicability::MachineApplicable;
if_chain!(
if let ExprKind::Lit(spanned) = &join_arg.kind;
if let LitKind::Str(symbol, _) = spanned.node;
if symbol.as_str().starts_with('/');
then {
span_lint_and_sugg(
cx,
PATH_JOIN_CORRECTION,
span.with_hi(expr.span.hi()),
r#"argument in join called on path contains a starting '/'"#,
"try removing first '/'",
"join(\"your/path/here\")".to_owned(),
applicability
);
}
);
}
15 changes: 15 additions & 0 deletions tests/ui/path_join_correction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![allow(unused)]
#![warn(clippy::path_join_correction)]

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

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

}
10 changes: 10 additions & 0 deletions tests/ui/path_join_correction.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: argument in join called on path contains a starting '/'
--> $DIR/path_join_correction.rs:7:8
|
LL | path.join("/sh");
| ^^^^^^^^^^^ help: try removing first '/': `join("your/path/here")`
|
= note: `-D clippy::path-join-correction` implied by `-D warnings`

error: aborting due to previous error

0 comments on commit 9a7bf41

Please sign in to comment.