Skip to content

Commit 9e2138d

Browse files
Auto merge of #147421 - Kivooeo:ice-fix51621, r=<try>
Add check if span is from macro expansion try-job: aarch64-msvc-1
2 parents 2170b4d + e1b361c commit 9e2138d

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

compiler/rustc_lint/src/shadowed_into_iter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ impl<'tcx> LateLintPass<'tcx> for ShadowedIntoIter {
134134
&& let hir::ExprKind::Call(path, [_]) = &arg.kind
135135
&& let hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::IntoIterIntoIter, ..)) =
136136
&path.kind
137+
&& !receiver_arg.span.from_expansion()
138+
&& !expr.span.from_expansion()
137139
{
138140
Some(ShadowedIntoIterDiagSub::RemoveIntoIter {
139141
span: receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ check-pass
2+
3+
fn main() {
4+
macro_rules! mac {
5+
(iter $e:expr) => {
6+
$e.iter()
7+
};
8+
(into_iter $e:expr) => {
9+
$e.into_iter() //~ WARN this method call resolves to
10+
//~^ WARN this changes meaning in Rust 2021
11+
};
12+
(next $e:expr) => {
13+
$e.iter().next()
14+
};
15+
}
16+
17+
for _ in dbg!([1, 2]).iter() {}
18+
for _ in dbg!([1, 2]).into_iter() {} //~ WARN this method call resolves to
19+
//~^ WARN this changes meaning in Rust 2021
20+
for _ in mac!(iter [1, 2]) {}
21+
for _ in mac!(into_iter [1, 2]) {}
22+
for _ in mac!(next [1, 2]) {} //~ WARN for loop over an
23+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
WARN rustc_errors::emitter Invalid span $SRC_DIR/std/src/macros.rs:LL:COL (#11), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/std/src/macros.rs" }) }
2+
warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021
3+
--> $DIR/macro-expansion-empty-span-147408.rs:18:27
4+
|
5+
LL | for _ in dbg!([1, 2]).into_iter() {}
6+
| ^^^^^^^^^
7+
|
8+
= warning: this changes meaning in Rust 2021
9+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2021/IntoIterator-for-arrays.html>
10+
= note: `#[warn(array_into_iter)]` (part of `#[warn(rust_2021_compatibility)]`) on by default
11+
help: use `.iter()` instead of `.into_iter()` to avoid ambiguity
12+
|
13+
LL - for _ in dbg!([1, 2]).into_iter() {}
14+
LL + for _ in dbg!([1, 2]).iter() {}
15+
|
16+
17+
warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021
18+
--> $DIR/macro-expansion-empty-span-147408.rs:9:16
19+
|
20+
LL | $e.into_iter()
21+
| ^^^^^^^^^
22+
...
23+
LL | for _ in mac!(into_iter [1, 2]) {}
24+
| ---------------------- in this macro invocation
25+
|
26+
= warning: this changes meaning in Rust 2021
27+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2021/IntoIterator-for-arrays.html>
28+
= note: this warning originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
29+
help: use `.iter()` instead of `.into_iter()` to avoid ambiguity
30+
|
31+
LL - $e.into_iter()
32+
LL + $e.iter()
33+
|
34+
help: or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value
35+
|
36+
LL | IntoIterator::into_iter($e.into_iter())
37+
| ++++++++++++++++++++++++ +
38+
39+
warning: for loop over an `Option`. This is more readably written as an `if let` statement
40+
--> $DIR/macro-expansion-empty-span-147408.rs:22:14
41+
|
42+
LL | for _ in mac!(next [1, 2]) {}
43+
| ^^^^^^^^^^^^^^^^^
44+
|
45+
= note: `#[warn(for_loops_over_fallibles)]` on by default
46+
help: to iterate over `$e.iter()` remove the call to `next`
47+
|
48+
LL - $e.iter().next()
49+
LL + .by_ref().next()
50+
|
51+
help: consider using `if let` to clear intent
52+
|
53+
LL - for _ in mac!(next [1, 2]) {}
54+
LL + if let Some(_) = mac!(next [1, 2]) {}
55+
|
56+
57+
warning: 3 warnings emitted
58+

0 commit comments

Comments
 (0)