-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
New lint to detect &std::path::MAIN_SEPARATOR.to_string()
#10483
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 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 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 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 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,72 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::msrvs::{self, Msrv}; | ||
use clippy_utils::{is_trait_method, match_def_path, paths, peel_hir_expr_refs}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::def::{DefKind, Res}; | ||
use rustc_hir::*; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for references on `std::path::MAIN_SEPARATOR.to_string()` used | ||
/// to build a `&str`. | ||
/// | ||
/// ### Why is this bad? | ||
/// There exists a `std::path::MAIN_SEPARATOR_STR` which does not require | ||
/// an extra memory allocation. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// let s: &str = &std::path::MAIN_SEPARATOR.to_string(); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// let s: &str = std::path::MAIN_SEPARATOR_STR; | ||
/// ``` | ||
#[clippy::version = "1.70.0"] | ||
pub MANUAL_MAIN_SEPARATOR_STR, | ||
complexity, | ||
"`&std::path::MAIN_SEPARATOR.to_string()` can be replaced by `std::path::MAIN_SEPARATOR_STR`" | ||
} | ||
|
||
pub struct ManualMainSeparatorStr { | ||
msrv: Msrv, | ||
} | ||
|
||
impl ManualMainSeparatorStr { | ||
#[must_use] | ||
pub fn new(msrv: Msrv) -> Self { | ||
Self { msrv } | ||
} | ||
} | ||
|
||
impl_lint_pass!(ManualMainSeparatorStr => [MANUAL_MAIN_SEPARATOR_STR]); | ||
|
||
impl LateLintPass<'_> for ManualMainSeparatorStr { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if self.msrv.meets(msrvs::PATH_MAIN_SEPARATOR_STR) && | ||
let (target, _) = peel_hir_expr_refs(expr) && | ||
is_trait_method(cx, target, sym::ToString) && | ||
let ExprKind::MethodCall(path, receiver, &[], _) = target.kind && | ||
path.ident.name == sym::to_string && | ||
let ExprKind::Path(QPath::Resolved(None, path)) = receiver.kind && | ||
let Res::Def(DefKind::Const, receiver_def_id) = path.res && | ||
match_def_path(cx, receiver_def_id, &paths::PATH_MAIN_SEPARATOR) && | ||
let ty::Ref(_, ty, Mutability::Not) = cx.typeck_results().expr_ty_adjusted(expr).kind() && | ||
ty.is_str() | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_MAIN_SEPARATOR_STR, | ||
expr.span, | ||
"taking a reference on `std::path::MAIN_SEPARATOR` conversion to `String`", | ||
"replace with", | ||
"std::path::MAIN_SEPARATOR_STR".to_owned(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} |
This file contains 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 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 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,39 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused)] | ||
#![warn(clippy::manual_main_separator_str)] | ||
|
||
use std::path::MAIN_SEPARATOR; | ||
|
||
fn len(s: &str) -> usize { | ||
s.len() | ||
} | ||
|
||
struct U<'a> { | ||
f: &'a str, | ||
g: &'a String, | ||
} | ||
|
||
struct V<T> { | ||
f: T, | ||
} | ||
|
||
fn main() { | ||
// Should lint | ||
let _: &str = std::path::MAIN_SEPARATOR_STR; | ||
let _ = len(std::path::MAIN_SEPARATOR_STR); | ||
let _: Vec<u16> = std::path::MAIN_SEPARATOR_STR.encode_utf16().collect(); | ||
|
||
// Should lint for field `f` only | ||
let _ = U { | ||
f: std::path::MAIN_SEPARATOR_STR, | ||
g: &MAIN_SEPARATOR.to_string(), | ||
}; | ||
|
||
// Should not lint | ||
let _: &String = &MAIN_SEPARATOR.to_string(); | ||
let _ = &MAIN_SEPARATOR.to_string(); | ||
let _ = V { | ||
f: &MAIN_SEPARATOR.to_string(), | ||
}; | ||
} |
This file contains 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,39 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused)] | ||
#![warn(clippy::manual_main_separator_str)] | ||
|
||
use std::path::MAIN_SEPARATOR; | ||
|
||
fn len(s: &str) -> usize { | ||
s.len() | ||
} | ||
|
||
struct U<'a> { | ||
f: &'a str, | ||
g: &'a String, | ||
} | ||
|
||
struct V<T> { | ||
f: T, | ||
} | ||
|
||
fn main() { | ||
// Should lint | ||
let _: &str = &MAIN_SEPARATOR.to_string(); | ||
let _ = len(&MAIN_SEPARATOR.to_string()); | ||
let _: Vec<u16> = MAIN_SEPARATOR.to_string().encode_utf16().collect(); | ||
|
||
// Should lint for field `f` only | ||
let _ = U { | ||
f: &MAIN_SEPARATOR.to_string(), | ||
g: &MAIN_SEPARATOR.to_string(), | ||
}; | ||
|
||
// Should not lint | ||
let _: &String = &MAIN_SEPARATOR.to_string(); | ||
let _ = &MAIN_SEPARATOR.to_string(); | ||
let _ = V { | ||
f: &MAIN_SEPARATOR.to_string(), | ||
}; | ||
} |
This file contains 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,28 @@ | ||
error: taking a reference on `std::path::MAIN_SEPARATOR` conversion to `String` | ||
--> $DIR/manual_main_separator_str.rs:23:19 | ||
| | ||
LL | let _: &str = &MAIN_SEPARATOR.to_string(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::path::MAIN_SEPARATOR_STR` | ||
| | ||
= note: `-D clippy::manual-main-separator-str` implied by `-D warnings` | ||
|
||
error: taking a reference on `std::path::MAIN_SEPARATOR` conversion to `String` | ||
--> $DIR/manual_main_separator_str.rs:24:17 | ||
| | ||
LL | let _ = len(&MAIN_SEPARATOR.to_string()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::path::MAIN_SEPARATOR_STR` | ||
|
||
error: taking a reference on `std::path::MAIN_SEPARATOR` conversion to `String` | ||
--> $DIR/manual_main_separator_str.rs:25:23 | ||
| | ||
LL | let _: Vec<u16> = MAIN_SEPARATOR.to_string().encode_utf16().collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::path::MAIN_SEPARATOR_STR` | ||
|
||
error: taking a reference on `std::path::MAIN_SEPARATOR` conversion to `String` | ||
--> $DIR/manual_main_separator_str.rs:29:12 | ||
| | ||
LL | f: &MAIN_SEPARATOR.to_string(), | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::path::MAIN_SEPARATOR_STR` | ||
|
||
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.
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.
What about
let
s without a given type? Or other expressions that are not direct local bindings?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.
I have added new tests, such as initializing a
struct
field with this expression or passing it as a function argument.I've also made it work when it is a method receiver (see the test with
encode_utf16()
), without an explicit&
.