-
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 inefficient_to_string
lint
#4683
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,55 @@ | ||
use super::INEFFICIENT_TO_STRING; | ||
use crate::utils::{match_def_path, paths, snippet_with_applicability, span_lint_and_then, walk_ptrs_ty_depth}; | ||
use if_chain::if_chain; | ||
use rustc::hir; | ||
use rustc::lint::LateContext; | ||
use rustc::ty::{self, Ty}; | ||
use rustc_errors::Applicability; | ||
|
||
/// Checks for the `INEFFICIENT_TO_STRING` lint | ||
pub fn lint<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &hir::Expr, arg: &hir::Expr, arg_ty: Ty<'tcx>) { | ||
if_chain! { | ||
if let Some(to_string_meth_did) = cx.tables.type_dependent_def_id(expr.hir_id); | ||
if match_def_path(cx, to_string_meth_did, &paths::TO_STRING_METHOD); | ||
if let Some(substs) = cx.tables.node_substs_opt(expr.hir_id); | ||
let self_ty = substs.type_at(0); | ||
let (deref_self_ty, deref_count) = walk_ptrs_ty_depth(self_ty); | ||
if deref_count >= 1; | ||
if specializes_tostring(cx, deref_self_ty); | ||
then { | ||
span_lint_and_then( | ||
cx, | ||
INEFFICIENT_TO_STRING, | ||
expr.span, | ||
&format!("calling `to_string` on `{}`", arg_ty), | ||
|db| { | ||
db.help(&format!( | ||
"`{}` implements `ToString` through a slower blanket impl, but `{}` has a fast specialization of `ToString`", | ||
self_ty, deref_self_ty | ||
)); | ||
let mut applicability = Applicability::MachineApplicable; | ||
let arg_snippet = snippet_with_applicability(cx, arg.span, "..", &mut applicability); | ||
db.span_suggestion( | ||
expr.span, | ||
"try dereferencing the receiver", | ||
format!("({}{}).to_string()", "*".repeat(deref_count), arg_snippet), | ||
applicability, | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
|
||
/// Returns whether `ty` specializes `ToString`. | ||
/// Currently, these are `str`, `String`, and `Cow<'_, str>`. | ||
fn specializes_tostring(cx: &LateContext<'_, '_>, ty: Ty<'_>) -> bool { | ||
match ty.kind { | ||
ty::Str => true, | ||
ty::Adt(adt, substs) => { | ||
match_def_path(cx, adt.did, &paths::STRING) | ||
|| (match_def_path(cx, adt.did, &paths::COW) && substs.type_at(1).is_str()) | ||
}, | ||
_ => 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
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,32 @@ | ||
// run-rustfix | ||
#![deny(clippy::inefficient_to_string)] | ||
|
||
use std::borrow::Cow; | ||
use std::string::ToString; | ||
|
||
fn main() { | ||
let rstr: &str = "hello"; | ||
let rrstr: &&str = &rstr; | ||
let rrrstr: &&&str = &rrstr; | ||
let _: String = rstr.to_string(); | ||
let _: String = (*rrstr).to_string(); | ||
let _: String = (**rrrstr).to_string(); | ||
|
||
let string: String = String::from("hello"); | ||
let rstring: &String = &string; | ||
let rrstring: &&String = &rstring; | ||
let rrrstring: &&&String = &rrstring; | ||
let _: String = string.to_string(); | ||
let _: String = rstring.to_string(); | ||
let _: String = (*rrstring).to_string(); | ||
let _: String = (**rrrstring).to_string(); | ||
|
||
let cow: Cow<'_, str> = Cow::Borrowed("hello"); | ||
let rcow: &Cow<'_, str> = &cow; | ||
let rrcow: &&Cow<'_, str> = &rcow; | ||
let rrrcow: &&&Cow<'_, str> = &rrcow; | ||
let _: String = cow.to_string(); | ||
let _: String = rcow.to_string(); | ||
let _: String = (*rrcow).to_string(); | ||
let _: String = (**rrrcow).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,32 @@ | ||
// run-rustfix | ||
#![deny(clippy::inefficient_to_string)] | ||
|
||
use std::borrow::Cow; | ||
use std::string::ToString; | ||
|
||
fn main() { | ||
let rstr: &str = "hello"; | ||
let rrstr: &&str = &rstr; | ||
let rrrstr: &&&str = &rrstr; | ||
let _: String = rstr.to_string(); | ||
let _: String = rrstr.to_string(); | ||
let _: String = rrrstr.to_string(); | ||
|
||
let string: String = String::from("hello"); | ||
let rstring: &String = &string; | ||
let rrstring: &&String = &rstring; | ||
let rrrstring: &&&String = &rrstring; | ||
let _: String = string.to_string(); | ||
let _: String = rstring.to_string(); | ||
let _: String = rrstring.to_string(); | ||
let _: String = rrrstring.to_string(); | ||
|
||
let cow: Cow<'_, str> = Cow::Borrowed("hello"); | ||
let rcow: &Cow<'_, str> = &cow; | ||
let rrcow: &&Cow<'_, str> = &rcow; | ||
let rrrcow: &&&Cow<'_, str> = &rrcow; | ||
let _: String = cow.to_string(); | ||
let _: String = rcow.to_string(); | ||
let _: String = rrcow.to_string(); | ||
let _: String = rrrcow.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,55 @@ | ||
error: calling `to_string` on `&&str` | ||
--> $DIR/inefficient_to_string.rs:12:21 | ||
| | ||
LL | let _: String = rrstr.to_string(); | ||
| ^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(*rrstr).to_string()` | ||
| | ||
note: lint level defined here | ||
--> $DIR/inefficient_to_string.rs:2:9 | ||
| | ||
LL | #![deny(clippy::inefficient_to_string)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= help: `&str` implements `ToString` through a slower blanket impl, but `str` has a fast specialization of `ToString` | ||
|
||
error: calling `to_string` on `&&&str` | ||
--> $DIR/inefficient_to_string.rs:13:21 | ||
| | ||
LL | let _: String = rrrstr.to_string(); | ||
| ^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(**rrrstr).to_string()` | ||
| | ||
= help: `&&str` implements `ToString` through a slower blanket impl, but `str` has a fast specialization of `ToString` | ||
|
||
error: calling `to_string` on `&&std::string::String` | ||
--> $DIR/inefficient_to_string.rs:21:21 | ||
| | ||
LL | let _: String = rrstring.to_string(); | ||
| ^^^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(*rrstring).to_string()` | ||
| | ||
= help: `&std::string::String` implements `ToString` through a slower blanket impl, but `std::string::String` has a fast specialization of `ToString` | ||
|
||
error: calling `to_string` on `&&&std::string::String` | ||
--> $DIR/inefficient_to_string.rs:22:21 | ||
| | ||
LL | let _: String = rrrstring.to_string(); | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(**rrrstring).to_string()` | ||
| | ||
= help: `&&std::string::String` implements `ToString` through a slower blanket impl, but `std::string::String` has a fast specialization of `ToString` | ||
|
||
error: calling `to_string` on `&&std::borrow::Cow<'_, str>` | ||
--> $DIR/inefficient_to_string.rs:30:21 | ||
| | ||
LL | let _: String = rrcow.to_string(); | ||
| ^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(*rrcow).to_string()` | ||
| | ||
= help: `&std::borrow::Cow<'_, str>` implements `ToString` through a slower blanket impl, but `std::borrow::Cow<'_, str>` has a fast specialization of `ToString` | ||
|
||
error: calling `to_string` on `&&&std::borrow::Cow<'_, str>` | ||
--> $DIR/inefficient_to_string.rs:31:21 | ||
| | ||
LL | let _: String = rrrcow.to_string(); | ||
| ^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(**rrrcow).to_string()` | ||
| | ||
= help: `&&std::borrow::Cow<'_, str>` implements `ToString` through a slower blanket impl, but `std::borrow::Cow<'_, str>` has a fast specialization of `ToString` | ||
|
||
error: aborting due to 6 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.
Can you add
// run-rustfix
to make this arustfix
test?