Skip to content

Commit

Permalink
Auto merge of rust-lang#10153 - llogiq:box-default-trim-paths, r=Jarcho
Browse files Browse the repository at this point in the history
trim paths in `box_default`

This might help with rust-lang#10089, though I have not tested that yet. In any event, it keeps the suggestion short and to the point.

---
changelog: Trim paths in [`box_default`] suggestion
  • Loading branch information
bors committed Jan 6, 2023
2 parents 3f48ed5 + d3a50d2 commit 179a22f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/box_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_hir::{
Block, Expr, ExprKind, Local, Node, QPath, TyKind,
};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_middle::{lint::in_external_macro, ty::print::with_forced_trimmed_paths};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;

Expand Down Expand Up @@ -59,7 +59,7 @@ impl LateLintPass<'_> for BoxDefault {
if is_plain_default(arg_path) || given_type(cx, expr) {
"Box::default()".into()
} else {
format!("Box::<{arg_ty}>::default()")
with_forced_trimmed_paths!(format!("Box::<{arg_ty}>::default()"))
},
Applicability::MachineApplicable
);
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/box_default.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ macro_rules! outer {
fn main() {
let _string: Box<String> = Box::default();
let _byte = Box::<u8>::default();
let _vec = Box::<std::vec::Vec<u8>>::default();
let _vec = Box::<Vec<u8>>::default();
let _impl = Box::<ImplementsDefault>::default();
let _impl2 = Box::<ImplementsDefault>::default();
let _impl3: Box<ImplementsDefault> = Box::default();
let _own = Box::new(OwnDefault::default()); // should not lint
let _in_macro = outer!(Box::<std::string::String>::default());
let _string_default = outer!(Box::<std::string::String>::default());
let _in_macro = outer!(Box::<String>::default());
let _string_default = outer!(Box::<String>::default());
let _vec2: Box<Vec<ImplementsDefault>> = Box::default();
let _vec3: Box<Vec<bool>> = Box::default();
let _vec4: Box<_> = Box::<std::vec::Vec<bool>>::default();
let _vec4: Box<_> = Box::<Vec<bool>>::default();
let _more = ret_ty_fn();
call_ty_fn(Box::default());
}
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/box_default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ error: `Box::new(_)` of default value
--> $DIR/box_default.rs:24:16
|
LL | let _vec = Box::new(Vec::<u8>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<std::vec::Vec<u8>>::default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<Vec<u8>>::default()`

error: `Box::new(_)` of default value
--> $DIR/box_default.rs:25:17
Expand All @@ -40,13 +40,13 @@ error: `Box::new(_)` of default value
--> $DIR/box_default.rs:29:28
|
LL | let _in_macro = outer!(Box::new(String::new()));
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<std::string::String>::default()`
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<String>::default()`

error: `Box::new(_)` of default value
--> $DIR/box_default.rs:30:34
|
LL | let _string_default = outer!(Box::new(String::from("")));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<std::string::String>::default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<String>::default()`

error: `Box::new(_)` of default value
--> $DIR/box_default.rs:31:46
Expand All @@ -64,7 +64,7 @@ error: `Box::new(_)` of default value
--> $DIR/box_default.rs:33:25
|
LL | let _vec4: Box<_> = Box::new(Vec::from([false; 0]));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<std::vec::Vec<bool>>::default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<Vec<bool>>::default()`

error: `Box::new(_)` of default value
--> $DIR/box_default.rs:35:16
Expand Down

0 comments on commit 179a22f

Please sign in to comment.