-
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
Add new lint string_as_str
#9624
Closed
Closed
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
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,45 @@ | ||
use clippy_utils::get_parent_expr; | ||
use clippy_utils::sugg::Sugg; | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, ty::is_type_diagnostic_item}; | ||
use rustc_ast::BorrowKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym::String; | ||
|
||
use super::STRING_AS_STR; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>) { | ||
if is_string(cx, recv) && !is_within_borrowed_call(cx, expr) { | ||
let parent = get_parent_expr(cx, recv).unwrap(); | ||
let sugg = format!("&{}", Sugg::hir(cx, recv, "..").maybe_par()); | ||
span_lint_and_sugg( | ||
cx, | ||
STRING_AS_STR, | ||
parent.span, | ||
"used `as_str()` for reference", | ||
"replace it with", | ||
sugg, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
|
||
fn is_string(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { | ||
is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), String) | ||
} | ||
|
||
fn is_within_borrowed_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { | ||
if let Some(parent) = clippy_utils::get_parent_expr(cx, expr) { | ||
return matches!( | ||
parent.kind, | ||
ExprKind::Closure(..) | ||
| ExprKind::Tup(..) | ||
| ExprKind::Match(_, _, _) | ||
| ExprKind::AddrOf(BorrowKind::Ref, ..) | ||
| ExprKind::MethodCall(_, _, _, _) | ||
); | ||
} | ||
|
||
false | ||
} |
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,59 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::string_as_str)] | ||
|
||
use clippy_utils::consts::Constant; | ||
|
||
fn main() { | ||
let my_string = String::from("hey"); | ||
let s = my_string.as_str(); | ||
fn_with_str(my_string.as_str()); | ||
|
||
let fs = FakeString::default(); | ||
let ss = fs.as_str(); | ||
|
||
match my_string.as_str() { | ||
"hello" => (), | ||
"hey" => (), | ||
_ => (), | ||
} | ||
|
||
match (my_string.as_str(), my_string.as_str()) { | ||
("hello", ("hello")) => (), | ||
("hey", "hey") => (), | ||
_ => (), | ||
} | ||
|
||
const ARRAY: &[&str] = &["hello", "dump_hir"]; | ||
let s: String = "hello".to_string(); | ||
let exists = ARRAY.contains(&s.as_str()); | ||
|
||
let a = s.as_str().chars().map(|c| c.is_digit(2)).collect::<Vec<_>>(); | ||
|
||
let path = vec!["path1".to_string(), "path2".to_string()]; | ||
let path: Vec<&str> = path.iter().map(|x| x.as_str()).collect(); | ||
let snip = "String".to_owned(); | ||
let name = "alice".to_string(); | ||
let a: Option<&str> = match snip.split_once(" as ") { | ||
None => Some(snip.as_str()), | ||
Some((import, rename)) => { | ||
if rename.trim() == name { | ||
None | ||
} else { | ||
Some(import.trim()) | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
fn fn_with_str(my_string: &str) { | ||
println!("hey {my_string}"); | ||
} | ||
|
||
#[derive(Default)] | ||
struct FakeString(pub u32); | ||
|
||
impl FakeString { | ||
fn as_str(&self) -> String { | ||
self.0.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,22 @@ | ||
error: used `as_str()` for reference | ||
--> $DIR/string_as_str.rs:8:13 | ||
| | ||
LL | let s = my_string.as_str(); | ||
| ^^^^^^^^^^^^^^^^^^ help: replace it with: `&my_string` | ||
| | ||
= note: `-D clippy::string-as-str` implied by `-D warnings` | ||
|
||
error: used `as_str()` for reference | ||
--> $DIR/string_as_str.rs:9:17 | ||
| | ||
LL | fn_with_str(my_string.as_str()); | ||
| ^^^^^^^^^^^^^^^^^^ help: replace it with: `&my_string` | ||
|
||
error: used `as_str()` for reference | ||
--> $DIR/string_as_str.rs:37:22 | ||
| | ||
LL | None => Some(snip.as_str()), | ||
| ^^^^^^^^^^^^^ help: replace it with: `&snip` | ||
|
||
error: aborting due to 3 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.
I'm not sure what this function is supposed to do. It looks like it checks for really specific things in the tests you've written.
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.
Don't know how to not lint here
rust-clippy/clippy_lints/src/missing_enforced_import_rename.rs
Line 77 in 50f192f
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.
Why can't this be linted?
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.
Because using
&snip
there are different type on those two branch, one is &str and other &String, don't know if we can change the code in other wayThere 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'm not sure if this is a blocking issue since it's not a machine applicable lint.
This seems like a wrong inference from
rustc
because explicit typing fixes the issueThere 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.
Exactly... Don't sure next step to complete this lint :( 🤔