-
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 box-default
lint
#9511
Merged
Merged
add box-default
lint
#9511
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_help, is_default_equivalent, path_def_id}; | ||
use rustc_hir::{Expr, ExprKind, QPath}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// checks for `Box::new(T::default())`, which is better written as | ||
/// `Box::<T>::default()`. | ||
/// | ||
/// ### Why is this bad? | ||
/// First, it's more complex, involving two calls instead of one. | ||
/// Second, `Box::default()` can be faster | ||
/// [in certain cases](https://nnethercote.github.io/perf-book/standard-library-types.html#box). | ||
/// | ||
/// ### Known problems | ||
/// The lint may miss some cases (e.g. Box::new(String::from(""))). | ||
/// On the other hand, it will trigger on cases where the `default` | ||
/// code comes from a macro that does something different based on | ||
/// e.g. target operating system. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// let x: Box<String> = Box::new(Default::default()); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// let x: Box<String> = Box::default(); | ||
/// ``` | ||
#[clippy::version = "1.65.0"] | ||
pub BOX_DEFAULT, | ||
perf, | ||
"Using Box::new(T::default()) instead of Box::default()" | ||
} | ||
|
||
declare_lint_pass!(BoxDefault => [BOX_DEFAULT]); | ||
|
||
impl LateLintPass<'_> for BoxDefault { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if let ExprKind::Call(box_new, [arg]) = expr.kind | ||
&& let ExprKind::Path(QPath::TypeRelative(ty, seg)) = box_new.kind | ||
&& let ExprKind::Call(..) = arg.kind | ||
&& !in_external_macro(cx.sess(), expr.span) | ||
&& expr.span.eq_ctxt(arg.span) | ||
&& seg.ident.name == sym::new | ||
&& path_def_id(cx, ty) == cx.tcx.lang_items().owned_box() | ||
&& is_default_equivalent(cx, arg) | ||
{ | ||
span_lint_and_help( | ||
cx, | ||
BOX_DEFAULT, | ||
expr.span, | ||
"`Box::new(_)` of default value", | ||
None, | ||
"use `Box::default()` instead", | ||
); | ||
} | ||
} | ||
} |
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,23 @@ | ||
### What it does | ||
checks for `Box::new(T::default())`, which is better written as | ||
`Box::<T>::default()`. | ||
|
||
### Why is this bad? | ||
First, it's more complex, involving two calls instead of one. | ||
Second, `Box::default()` can be faster | ||
[in certain cases](https://nnethercote.github.io/perf-book/standard-library-types.html#box). | ||
|
||
### Known problems | ||
The lint may miss some cases (e.g. Box::new(String::from(""))). | ||
On the other hand, it will trigger on cases where the `default` | ||
code comes from a macro that does something different based on | ||
e.g. target operating system. | ||
|
||
### Example | ||
``` | ||
let x: Box<String> = Box::new(Default::default()); | ||
``` | ||
Use instead: | ||
``` | ||
let x: Box<String> = Box::default(); | ||
``` |
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,31 @@ | ||
#![warn(clippy::box_default)] | ||
|
||
#[derive(Default)] | ||
struct ImplementsDefault; | ||
|
||
struct OwnDefault; | ||
|
||
impl OwnDefault { | ||
fn default() -> Self { | ||
Self | ||
} | ||
} | ||
|
||
macro_rules! outer { | ||
($e: expr) => { | ||
$e | ||
}; | ||
} | ||
|
||
fn main() { | ||
let _string: Box<String> = Box::new(Default::default()); | ||
let _byte = Box::new(u8::default()); | ||
let _vec = Box::new(Vec::<u8>::new()); | ||
let _impl = Box::new(ImplementsDefault::default()); | ||
let _impl2 = Box::new(<ImplementsDefault as Default>::default()); | ||
let _impl3: Box<ImplementsDefault> = Box::new(Default::default()); | ||
let _own = Box::new(OwnDefault::default()); // should not lint | ||
let _in_macro = outer!(Box::new(String::new())); | ||
// false negative: default is from different expansion | ||
let _vec2: Box<Vec<ImplementsDefault>> = Box::new(vec![]); | ||
} |
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 @@ | ||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:21:32 | ||
| | ||
LL | let _string: Box<String> = Box::new(Default::default()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::box-default` implied by `-D warnings` | ||
= help: use `Box::default()` instead | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:22:17 | ||
| | ||
LL | let _byte = Box::new(u8::default()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:23:16 | ||
| | ||
LL | let _vec = Box::new(Vec::<u8>::new()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:24:17 | ||
| | ||
LL | let _impl = Box::new(ImplementsDefault::default()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:25:18 | ||
| | ||
LL | let _impl2 = Box::new(<ImplementsDefault as Default>::default()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:26:42 | ||
| | ||
LL | let _impl3: Box<ImplementsDefault> = Box::new(Default::default()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:28:28 | ||
| | ||
LL | let _in_macro = outer!(Box::new(String::new())); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
|
||
error: aborting due to 7 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.
Could add a check for
expr.span.ctxt() == arg.span.ctxt()
to avoid linting across macro boundaries while still catching it in local macro definitionsThere 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.
Good idea. Done.
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 remove the macro note from
Known problems
with that unless there's a case I'm missingThere 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.
Oh maybe things like
Box::new(cfg_if! ... { String::new() } else { ... })
where the expressions aren't coming from the macroThere 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. Though if you want, I can remove the note until I find a clearer wording.
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'd say remove it, apparently you can't use
cfg_if
in an expression position, nor#[cfg]
as attributes on expressions are unstable