-
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
unnecessary_owned_empty_strings
#8660
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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
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,81 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, ty::is_type_diagnostic_item}; | ||
use clippy_utils::{match_def_path, paths}; | ||
use if_chain::if_chain; | ||
use rustc_ast::ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// | ||
/// Detects cases of owned empty strings being passed as an argument to a function expecting `&str` | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// This results in longer and less readable code | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// vec!["1", "2", "3"].join(&String::new()); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// vec!["1", "2", "3"].join(""); | ||
/// ``` | ||
#[clippy::version = "1.62.0"] | ||
pub UNNECESSARY_OWNED_EMPTY_STRING, | ||
style, | ||
"detects cases of references to owned empty strings being passed as an argument to a function expecting `&str`" | ||
} | ||
declare_lint_pass!(UnnecessaryOwnedEmptyString => [UNNECESSARY_OWNED_EMPTY_STRING]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for UnnecessaryOwnedEmptyString { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
if_chain! { | ||
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner_expr) = expr.kind; | ||
if let ExprKind::Call(fun, args) = inner_expr.kind; | ||
if let ExprKind::Path(ref qpath) = fun.kind; | ||
if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id(); | ||
if let ty::Ref(_, inner_str, _) = cx.typeck_results().expr_ty_adjusted(expr).kind(); | ||
if inner_str.is_str(); | ||
then { | ||
if match_def_path(cx, fun_def_id, &paths::STRING_NEW) { | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_OWNED_EMPTY_STRING, | ||
expr.span, | ||
"usage of `&String::new()` for a function expecting a `&str` argument", | ||
"try", | ||
"\"\"".to_owned(), | ||
Applicability::MachineApplicable, | ||
); | ||
} else { | ||
if_chain! { | ||
if match_def_path(cx, fun_def_id, &paths::FROM_FROM); | ||
if let [.., last_arg] = args; | ||
if let ExprKind::Lit(spanned) = &last_arg.kind; | ||
if let LitKind::Str(symbol, _) = spanned.node; | ||
if symbol.is_empty(); | ||
let inner_expr_type = cx.typeck_results().expr_ty(inner_expr); | ||
if is_type_diagnostic_item(cx, inner_expr_type, sym::String); | ||
then { | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_OWNED_EMPTY_STRING, | ||
expr.span, | ||
"usage of `&String::from(\"\")` for a function expecting a `&str` argument", | ||
"try", | ||
"\"\"".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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::unnecessary_owned_empty_string)] | ||
|
||
fn ref_str_argument(_value: &str) {} | ||
|
||
#[allow(clippy::ptr_arg)] | ||
fn ref_string_argument(_value: &String) {} | ||
|
||
fn main() { | ||
// should be linted | ||
ref_str_argument(""); | ||
|
||
// should be linted | ||
ref_str_argument(""); | ||
|
||
// should not be linted | ||
ref_str_argument(""); | ||
|
||
// should not be linted | ||
ref_string_argument(&String::new()); | ||
} |
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 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::unnecessary_owned_empty_string)] | ||
|
||
fn ref_str_argument(_value: &str) {} | ||
|
||
#[allow(clippy::ptr_arg)] | ||
fn ref_string_argument(_value: &String) {} | ||
|
||
fn main() { | ||
// should be linted | ||
ref_str_argument(&String::new()); | ||
|
||
// should be linted | ||
ref_str_argument(&String::from("")); | ||
|
||
// should not be linted | ||
ref_str_argument(""); | ||
|
||
// should not be linted | ||
ref_string_argument(&String::new()); | ||
} |
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,16 @@ | ||
error: usage of `&String::new()` for a function expecting a `&str` argument | ||
--> $DIR/unnecessary_owned_empty_string.rs:12:22 | ||
| | ||
LL | ref_str_argument(&String::new()); | ||
| ^^^^^^^^^^^^^^ help: try: `""` | ||
| | ||
= note: `-D clippy::unnecessary-owned-empty-string` implied by `-D warnings` | ||
|
||
error: usage of `&String::from("")` for a function expecting a `&str` argument | ||
--> $DIR/unnecessary_owned_empty_string.rs:15:22 | ||
| | ||
LL | ref_str_argument(&String::from("")); | ||
| ^^^^^^^^^^^^^^^^^ help: try: `""` | ||
|
||
error: aborting due to 2 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.
Lint names are usually better in the plural form, when they aren't describing a specific function name. This usually reads better in combination with
allow
.This change will require
cargo dev update_lints
.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.
Done