Skip to content

Commit 9663da3

Browse files
Run UI tests in edition 2024 mode (rust-lang#14602)
The `ui_test` package runs test in edition 2021 mode by default. This PR switches our tests to edition 2024. The commits progressively make our test suite compatible with both edition 2021 and edition 2024, then switches the testing mode to edition 2024, which is compatible with what `cargo dev lint` also uses by default. The changes are (without functionality changes in tests): - Add `unsafe` when [calling unsafe constructs inside `unsafe fn`](https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-op-in-unsafe-fn.html), to [unsafe attributes](https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-attributes.html), and to [`extern` blocks](https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-extern.html). - Use stricter reference patterns to accomodate with the [new match ergonomics rules](https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html). - Add some `use<>` markers where required to keep the same semantics under the [new RPIT lifetime rules](https://doc.rust-lang.org/edition-guide/rust-2024/rpit-lifetime-capture.html). Some other changes ensure that non-regression tests are still enforced: - Add edition 2021 specific tests when switching rules would make some explicitly tested constructs untested, or when the test require using the older [temporary tail expression scoping rules](https://doc.rust-lang.org/edition-guide/rust-2024/temporary-tail-expr-scope.html). - In `misnamed_getters`, check that getters containing an `unsafe` block (for example a getter on an `Union` field) trigger the lint. The last commit switches the default edition for running UI tests to edition 2024. changelog: [`misnamed_getters`]: getters containing an `unsafe` block are also linted
2 parents 459897b + c7640e0 commit 9663da3

File tree

95 files changed

+1059
-781
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1059
-781
lines changed

clippy_lints/src/functions/misnamed_getters.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
33
use rustc_errors::Applicability;
44
use rustc_hir::intravisit::FnKind;
5-
use rustc_hir::{Body, ExprKind, FnDecl, ImplicitSelfKind};
5+
use rustc_hir::{BlockCheckMode, Body, ExprKind, FnDecl, ImplicitSelfKind, UnsafeSource};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty;
88
use rustc_span::Span;
@@ -40,14 +40,25 @@ pub fn check_fn(cx: &LateContext<'_>, kind: FnKind<'_>, decl: &FnDecl<'_>, body:
4040
name
4141
};
4242

43-
// Body must be &(mut) <self_data>.name
43+
// Body must be `&(mut) <self_data>.name`, potentially in an `unsafe` block
4444
// self_data is not necessarily self, to also lint sub-getters, etc…
4545

4646
let block_expr = if let ExprKind::Block(block, _) = body.value.kind
4747
&& block.stmts.is_empty()
4848
&& let Some(block_expr) = block.expr
4949
{
50-
block_expr
50+
if let ExprKind::Block(unsafe_block, _) = block_expr.kind
51+
&& unsafe_block.stmts.is_empty()
52+
&& matches!(
53+
unsafe_block.rules,
54+
BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided)
55+
)
56+
&& let Some(unsafe_block_expr) = unsafe_block.expr
57+
{
58+
unsafe_block_expr
59+
} else {
60+
block_expr
61+
}
5162
} else {
5263
return;
5364
};

tests/compile-test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use pulldown_cmark::{Options, Parser, html};
1414
use serde::Deserialize;
1515
use test_utils::IS_RUSTC_TEST_SUITE;
1616
use ui_test::custom_flags::Flag;
17+
use ui_test::custom_flags::edition::Edition;
1718
use ui_test::custom_flags::rustfix::RustfixMode;
1819
use ui_test::spanned::Spanned;
1920
use ui_test::{Args, CommandBuilder, Config, Match, error_on_output_conflict, status_emitter};
@@ -156,6 +157,7 @@ impl TestContext {
156157
..Config::rustc(Path::new("tests").join(test_dir))
157158
};
158159
let defaults = config.comment_defaults.base();
160+
defaults.set_custom("edition", Edition("2024".into()));
159161
defaults.exit_status = None.into();
160162
if mandatory_annotations {
161163
defaults.require_annotations = Some(Spanned::dummy(true)).into();

tests/ui-toml/strict_non_send_fields_in_send_ty/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ unsafe impl<T> Send for MyOption<T> {}
2929
//~^ non_send_fields_in_send_ty
3030

3131
// All fields are disallowed when raw pointer heuristic is off
32-
extern "C" {
32+
unsafe extern "C" {
3333
type NonSend;
3434
}
3535

tests/ui/asm_syntax_not_x86.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ mod dont_warn {
88
use std::arch::{asm, global_asm};
99

1010
pub(super) unsafe fn use_asm() {
11-
asm!("");
12-
asm!("", options());
13-
asm!("", options(nostack));
11+
unsafe {
12+
asm!("");
13+
asm!("", options());
14+
asm!("", options(nostack));
15+
}
1416
}
1517

1618
global_asm!("");

tests/ui/asm_syntax_x86.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ mod warn_intel {
55
use std::arch::{asm, global_asm};
66

77
pub(super) unsafe fn use_asm() {
8-
asm!("");
9-
//~^ inline_asm_x86_intel_syntax
8+
unsafe {
9+
asm!("");
10+
//~^ inline_asm_x86_intel_syntax
1011

11-
asm!("", options());
12-
//~^ inline_asm_x86_intel_syntax
12+
asm!("", options());
13+
//~^ inline_asm_x86_intel_syntax
1314

14-
asm!("", options(nostack));
15-
//~^ inline_asm_x86_intel_syntax
15+
asm!("", options(nostack));
16+
//~^ inline_asm_x86_intel_syntax
1617

17-
asm!("", options(att_syntax));
18-
asm!("", options(nostack, att_syntax));
18+
asm!("", options(att_syntax));
19+
asm!("", options(nostack, att_syntax));
20+
}
1921
}
2022

2123
global_asm!("");
@@ -32,14 +34,16 @@ mod warn_att {
3234
use std::arch::{asm, global_asm};
3335

3436
pub(super) unsafe fn use_asm() {
35-
asm!("");
36-
asm!("", options());
37-
asm!("", options(nostack));
38-
asm!("", options(att_syntax));
39-
//~^ inline_asm_x86_att_syntax
40-
41-
asm!("", options(nostack, att_syntax));
42-
//~^ inline_asm_x86_att_syntax
37+
unsafe {
38+
asm!("");
39+
asm!("", options());
40+
asm!("", options(nostack));
41+
asm!("", options(att_syntax));
42+
//~^ inline_asm_x86_att_syntax
43+
44+
asm!("", options(nostack, att_syntax));
45+
//~^ inline_asm_x86_att_syntax
46+
}
4347
}
4448

4549
global_asm!("");

tests/ui/asm_syntax_x86.stderr

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
11
error: Intel x86 assembly syntax used
2-
--> tests/ui/asm_syntax_x86.rs:8:9
2+
--> tests/ui/asm_syntax_x86.rs:9:13
33
|
4-
LL | asm!("");
5-
| ^^^^^^^^
4+
LL | asm!("");
5+
| ^^^^^^^^
66
|
77
= help: use AT&T x86 assembly syntax
88
= note: `-D clippy::inline-asm-x86-intel-syntax` implied by `-D warnings`
99
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_intel_syntax)]`
1010

1111
error: Intel x86 assembly syntax used
12-
--> tests/ui/asm_syntax_x86.rs:11:9
12+
--> tests/ui/asm_syntax_x86.rs:12:13
1313
|
14-
LL | asm!("", options());
15-
| ^^^^^^^^^^^^^^^^^^^
14+
LL | asm!("", options());
15+
| ^^^^^^^^^^^^^^^^^^^
1616
|
1717
= help: use AT&T x86 assembly syntax
1818

1919
error: Intel x86 assembly syntax used
20-
--> tests/ui/asm_syntax_x86.rs:14:9
20+
--> tests/ui/asm_syntax_x86.rs:15:13
2121
|
22-
LL | asm!("", options(nostack));
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
LL | asm!("", options(nostack));
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2424
|
2525
= help: use AT&T x86 assembly syntax
2626

2727
error: Intel x86 assembly syntax used
28-
--> tests/ui/asm_syntax_x86.rs:21:5
28+
--> tests/ui/asm_syntax_x86.rs:23:5
2929
|
3030
LL | global_asm!("");
3131
| ^^^^^^^^^^^^^^^
3232
|
3333
= help: use AT&T x86 assembly syntax
3434

3535
error: Intel x86 assembly syntax used
36-
--> tests/ui/asm_syntax_x86.rs:24:5
36+
--> tests/ui/asm_syntax_x86.rs:26:5
3737
|
3838
LL | global_asm!("", options());
3939
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4040
|
4141
= help: use AT&T x86 assembly syntax
4242

4343
error: AT&T x86 assembly syntax used
44-
--> tests/ui/asm_syntax_x86.rs:38:9
44+
--> tests/ui/asm_syntax_x86.rs:41:13
4545
|
46-
LL | asm!("", options(att_syntax));
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
LL | asm!("", options(att_syntax));
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4848
|
4949
= help: use Intel x86 assembly syntax
5050
= note: `-D clippy::inline-asm-x86-att-syntax` implied by `-D warnings`
5151
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_att_syntax)]`
5252

5353
error: AT&T x86 assembly syntax used
54-
--> tests/ui/asm_syntax_x86.rs:41:9
54+
--> tests/ui/asm_syntax_x86.rs:44:13
5555
|
56-
LL | asm!("", options(nostack, att_syntax));
57-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
LL | asm!("", options(nostack, att_syntax));
57+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5858
|
5959
= help: use Intel x86 assembly syntax
6060

6161
error: AT&T x86 assembly syntax used
62-
--> tests/ui/asm_syntax_x86.rs:47:5
62+
--> tests/ui/asm_syntax_x86.rs:51:5
6363
|
6464
LL | global_asm!("", options(att_syntax));
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/blocks_in_conditions.fixed

+1-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
//@aux-build:proc_macro_attr.rs
22

33
#![warn(clippy::blocks_in_conditions)]
4-
#![allow(
5-
unused,
6-
clippy::let_and_return,
7-
clippy::needless_if,
8-
clippy::missing_transmute_annotations
9-
)]
4+
#![allow(unused, clippy::needless_if, clippy::missing_transmute_annotations)]
105
#![warn(clippy::nonminimal_bool)]
116

127
macro_rules! blocky {
@@ -71,28 +66,6 @@ fn block_in_assert() {
7166
);
7267
}
7368

74-
// issue #11814
75-
fn block_in_match_expr(num: i32) -> i32 {
76-
let res = {
77-
//~^ ERROR: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
78-
let opt = Some(2);
79-
opt
80-
}; match res {
81-
Some(0) => 1,
82-
Some(n) => num * 2,
83-
None => 0,
84-
};
85-
86-
match unsafe {
87-
let hearty_hearty_hearty = vec![240, 159, 146, 150];
88-
String::from_utf8_unchecked(hearty_hearty_hearty).as_str()
89-
} {
90-
"💖" => 1,
91-
"what" => 2,
92-
_ => 3,
93-
}
94-
}
95-
9669
// issue #12162
9770
macro_rules! timed {
9871
($name:expr, $body:expr $(,)?) => {{

tests/ui/blocks_in_conditions.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
//@aux-build:proc_macro_attr.rs
22

33
#![warn(clippy::blocks_in_conditions)]
4-
#![allow(
5-
unused,
6-
clippy::let_and_return,
7-
clippy::needless_if,
8-
clippy::missing_transmute_annotations
9-
)]
4+
#![allow(unused, clippy::needless_if, clippy::missing_transmute_annotations)]
105
#![warn(clippy::nonminimal_bool)]
116

127
macro_rules! blocky {
@@ -71,28 +66,6 @@ fn block_in_assert() {
7166
);
7267
}
7368

74-
// issue #11814
75-
fn block_in_match_expr(num: i32) -> i32 {
76-
match {
77-
//~^ ERROR: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
78-
let opt = Some(2);
79-
opt
80-
} {
81-
Some(0) => 1,
82-
Some(n) => num * 2,
83-
None => 0,
84-
};
85-
86-
match unsafe {
87-
let hearty_hearty_hearty = vec![240, 159, 146, 150];
88-
String::from_utf8_unchecked(hearty_hearty_hearty).as_str()
89-
} {
90-
"💖" => 1,
91-
"what" => 2,
92-
_ => 3,
93-
}
94-
}
95-
9669
// issue #12162
9770
macro_rules! timed {
9871
($name:expr, $body:expr $(,)?) => {{

tests/ui/blocks_in_conditions.stderr

+4-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
2-
--> tests/ui/blocks_in_conditions.rs:30:5
2+
--> tests/ui/blocks_in_conditions.rs:25:5
33
|
44
LL | / if {
55
LL | |
@@ -20,38 +20,19 @@ LL ~ }; if res {
2020
|
2121

2222
error: omit braces around single expression condition
23-
--> tests/ui/blocks_in_conditions.rs:42:8
23+
--> tests/ui/blocks_in_conditions.rs:37:8
2424
|
2525
LL | if { true } { 6 } else { 10 }
2626
| ^^^^^^^^ help: try: `true`
2727

2828
error: this boolean expression can be simplified
29-
--> tests/ui/blocks_in_conditions.rs:48:8
29+
--> tests/ui/blocks_in_conditions.rs:43:8
3030
|
3131
LL | if true && x == 3 { 6 } else { 10 }
3232
| ^^^^^^^^^^^^^^ help: try: `x == 3`
3333
|
3434
= note: `-D clippy::nonminimal-bool` implied by `-D warnings`
3535
= help: to override `-D warnings` add `#[allow(clippy::nonminimal_bool)]`
3636

37-
error: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
38-
--> tests/ui/blocks_in_conditions.rs:76:5
39-
|
40-
LL | / match {
41-
LL | |
42-
LL | | let opt = Some(2);
43-
LL | | opt
44-
LL | | } {
45-
| |_____^
46-
|
47-
help: try
48-
|
49-
LL ~ let res = {
50-
LL +
51-
LL + let opt = Some(2);
52-
LL + opt
53-
LL ~ }; match res {
54-
|
55-
56-
error: aborting due to 4 previous errors
37+
error: aborting due to 3 previous errors
5738

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@edition: 2021
2+
3+
#![allow(clippy::let_and_return)]
4+
5+
// issue #11814
6+
fn block_in_match_expr(num: i32) -> i32 {
7+
let res = {
8+
//~^ ERROR: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
9+
let opt = Some(2);
10+
opt
11+
}; match res {
12+
Some(0) => 1,
13+
Some(n) => num * 2,
14+
None => 0,
15+
};
16+
17+
match unsafe {
18+
let hearty_hearty_hearty = vec![240, 159, 146, 150];
19+
String::from_utf8_unchecked(hearty_hearty_hearty).as_str()
20+
} {
21+
"💖" => 1,
22+
"what" => 2,
23+
_ => 3,
24+
}
25+
}

0 commit comments

Comments
 (0)