Skip to content

Commit

Permalink
Auto merge of rust-lang#108471 - clubby789:unbox-the-syntax, r=Nilstr…
Browse files Browse the repository at this point in the history
…ieb,est31

Remove `box_syntax`

r? `@Nilstrieb`

This removes the feature `box_syntax`, which allows the use of `box <expr>` to create a Box, and finalises removing use of the feature from the compiler. `box_patterns` (allowing the use of `box <pat>` in a pattern) is unaffected.
It also removes `ast::ExprKind::Box` - the only way to create a 'box' expression now is with the rustc-internal `#[rustc_box]` attribute.
As a temporary measure to help users move away, `box <expr>` now parses the inner expression, and emits a `MachineApplicable` lint to replace it with `Box::new`

Closes rust-lang#49733
  • Loading branch information
bors committed Mar 13, 2023
2 parents e07c6b4 + 15f2423 commit ff23e48
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 85 deletions.
3 changes: 1 addition & 2 deletions clippy_lints/src/suspicious_operation_groupings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,7 @@ fn ident_difference_expr_with_base_location(
| (MethodCall(_), MethodCall(_))
| (Call(_, _), Call(_, _))
| (ConstBlock(_), ConstBlock(_))
| (Array(_), Array(_))
| (Box(_), Box(_)) => {
| (Array(_), Array(_)) => {
// keep going
},
_ => {
Expand Down
2 changes: 1 addition & 1 deletion clippy_utils/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
(Paren(l), _) => eq_expr(l, r),
(_, Paren(r)) => eq_expr(l, r),
(Err, Err) => true,
(Box(l), Box(r)) | (Try(l), Try(r)) | (Await(l), Await(r)) => eq_expr(l, r),
(Try(l), Try(r)) | (Await(l), Await(r)) => eq_expr(l, r),
(Array(l), Array(r)) => over(l, r, |l, r| eq_expr(l, r)),
(Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)),
(Repeat(le, ls), Repeat(re, rs)) => eq_expr(le, re) && eq_expr(&ls.value, &rs.value),
Expand Down
1 change: 0 additions & 1 deletion clippy_utils/src/sugg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ impl<'a> Sugg<'a> {
match expr.kind {
_ if expr.span.ctxt() != ctxt => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0),
ast::ExprKind::AddrOf(..)
| ast::ExprKind::Box(..)
| ast::ExprKind::Closure { .. }
| ast::ExprKind::If(..)
| ast::ExprKind::Let(..)
Expand Down
35 changes: 12 additions & 23 deletions tests/ui/boxed_local.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(box_syntax)]
#![feature(lint_reasons)]
#![allow(
clippy::borrowed_box,
Expand Down Expand Up @@ -34,7 +33,7 @@ fn ok_box_trait(boxed_trait: &Box<dyn Z>) {
}

fn warn_call() {
let x = box A;
let x = Box::new(A);
x.foo();
}

Expand All @@ -43,41 +42,41 @@ fn warn_arg(x: Box<A>) {
}

fn nowarn_closure_arg() {
let x = Some(box A);
let x = Some(Box::new(A));
x.map_or((), |x| take_ref(&x));
}

fn warn_rename_call() {
let x = box A;
let x = Box::new(A);

let y = x;
y.foo(); // via autoderef
}

fn warn_notuse() {
let bz = box A;
let bz = Box::new(A);
}

fn warn_pass() {
let bz = box A;
let bz = Box::new(A);
take_ref(&bz); // via deref coercion
}

fn nowarn_return() -> Box<A> {
box A // moved out, "escapes"
Box::new(A) // moved out, "escapes"
}

fn nowarn_move() {
let bx = box A;
let bx = Box::new(A);
drop(bx) // moved in, "escapes"
}
fn nowarn_call() {
let bx = box A;
let bx = Box::new(A);
bx.clone(); // method only available to Box, not via autoderef
}

fn nowarn_pass() {
let bx = box A;
let bx = Box::new(A);
take_box(&bx); // fn needs &Box
}

Expand All @@ -86,30 +85,20 @@ fn take_ref(x: &A) {}

fn nowarn_ref_take() {
// false positive, should actually warn
let x = box A;
let x = Box::new(A);
let y = &x;
take_box(y);
}

fn nowarn_match() {
let x = box A; // moved into a match
let x = Box::new(A); // moved into a match
match x {
y => drop(y),
}
}

fn warn_match() {
let x = box A;
match &x {
// not moved
y => (),
}
}

fn nowarn_large_array() {
// should not warn, is large array
// and should not be on stack
let x = box [1; 10000];
let x = Box::new(A);
match &x {
// not moved
y => (),
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/boxed_local.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
error: local variable doesn't need to be boxed here
--> $DIR/boxed_local.rs:41:13
--> $DIR/boxed_local.rs:40:13
|
LL | fn warn_arg(x: Box<A>) {
| ^
|
= note: `-D clippy::boxed-local` implied by `-D warnings`

error: local variable doesn't need to be boxed here
--> $DIR/boxed_local.rs:132:12
--> $DIR/boxed_local.rs:121:12
|
LL | pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {}
| ^^^^^^^^^^^

error: local variable doesn't need to be boxed here
--> $DIR/boxed_local.rs:196:44
--> $DIR/boxed_local.rs:185:44
|
LL | fn default_impl_x(self: Box<Self>, x: Box<u32>) -> u32 {
| ^

error: local variable doesn't need to be boxed here
--> $DIR/boxed_local.rs:203:16
--> $DIR/boxed_local.rs:192:16
|
LL | fn foo(x: Box<u32>) {}
| ^
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/no_effect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(box_syntax, fn_traits, unboxed_closures)]
#![feature(fn_traits, unboxed_closures)]
#![warn(clippy::no_effect_underscore_binding)]
#![allow(dead_code, path_statements)]
#![allow(clippy::deref_addrof, clippy::redundant_field_names, clippy::uninlined_format_args)]
Expand Down Expand Up @@ -102,7 +102,6 @@ fn main() {
*&42;
&6;
(5, 6, 7);
box 42;
..;
5..;
..5;
Expand Down
38 changes: 16 additions & 22 deletions tests/ui/no_effect.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -81,106 +81,100 @@ LL | (5, 6, 7);
error: statement with no effect
--> $DIR/no_effect.rs:105:5
|
LL | box 42;
| ^^^^^^^

error: statement with no effect
--> $DIR/no_effect.rs:106:5
|
LL | ..;
| ^^^

error: statement with no effect
--> $DIR/no_effect.rs:107:5
--> $DIR/no_effect.rs:106:5
|
LL | 5..;
| ^^^^

error: statement with no effect
--> $DIR/no_effect.rs:108:5
--> $DIR/no_effect.rs:107:5
|
LL | ..5;
| ^^^^

error: statement with no effect
--> $DIR/no_effect.rs:109:5
--> $DIR/no_effect.rs:108:5
|
LL | 5..6;
| ^^^^^

error: statement with no effect
--> $DIR/no_effect.rs:110:5
--> $DIR/no_effect.rs:109:5
|
LL | 5..=6;
| ^^^^^^

error: statement with no effect
--> $DIR/no_effect.rs:111:5
--> $DIR/no_effect.rs:110:5
|
LL | [42, 55];
| ^^^^^^^^^

error: statement with no effect
--> $DIR/no_effect.rs:112:5
--> $DIR/no_effect.rs:111:5
|
LL | [42, 55][1];
| ^^^^^^^^^^^^

error: statement with no effect
--> $DIR/no_effect.rs:113:5
--> $DIR/no_effect.rs:112:5
|
LL | (42, 55).1;
| ^^^^^^^^^^^

error: statement with no effect
--> $DIR/no_effect.rs:114:5
--> $DIR/no_effect.rs:113:5
|
LL | [42; 55];
| ^^^^^^^^^

error: statement with no effect
--> $DIR/no_effect.rs:115:5
--> $DIR/no_effect.rs:114:5
|
LL | [42; 55][13];
| ^^^^^^^^^^^^^

error: statement with no effect
--> $DIR/no_effect.rs:117:5
--> $DIR/no_effect.rs:116:5
|
LL | || x += 5;
| ^^^^^^^^^^

error: statement with no effect
--> $DIR/no_effect.rs:119:5
--> $DIR/no_effect.rs:118:5
|
LL | FooString { s: s };
| ^^^^^^^^^^^^^^^^^^^

error: binding to `_` prefixed variable with no side-effect
--> $DIR/no_effect.rs:120:5
--> $DIR/no_effect.rs:119:5
|
LL | let _unused = 1;
| ^^^^^^^^^^^^^^^^
|
= note: `-D clippy::no-effect-underscore-binding` implied by `-D warnings`

error: binding to `_` prefixed variable with no side-effect
--> $DIR/no_effect.rs:121:5
--> $DIR/no_effect.rs:120:5
|
LL | let _penguin = || println!("Some helpful closure");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: binding to `_` prefixed variable with no side-effect
--> $DIR/no_effect.rs:122:5
--> $DIR/no_effect.rs:121:5
|
LL | let _duck = Struct { field: 0 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: binding to `_` prefixed variable with no side-effect
--> $DIR/no_effect.rs:123:5
--> $DIR/no_effect.rs:122:5
|
LL | let _cat = [2, 4, 6, 8][2];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 30 previous errors
error: aborting due to 29 previous errors

2 changes: 0 additions & 2 deletions tests/ui/unnecessary_operation.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// run-rustfix

#![feature(box_syntax)]
#![allow(clippy::deref_addrof, dead_code, unused, clippy::no_effect)]
#![warn(clippy::unnecessary_operation)]

Expand Down Expand Up @@ -59,7 +58,6 @@ fn main() {
5;6;get_number();
get_number();
get_number();
get_number();
5;get_number();
42;get_number();
assert!([42, 55].len() > get_usize());
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/unnecessary_operation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// run-rustfix

#![feature(box_syntax)]
#![allow(clippy::deref_addrof, dead_code, unused, clippy::no_effect)]
#![warn(clippy::unnecessary_operation)]

Expand Down Expand Up @@ -57,7 +56,6 @@ fn main() {
*&get_number();
&get_number();
(5, 6, get_number());
box get_number();
get_number()..;
..get_number();
5..get_number();
Expand Down
Loading

0 comments on commit ff23e48

Please sign in to comment.