Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_lint/src/for_loops_over_fallibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
let Some((pat, arg)) = extract_for_loop(expr) else { return };

// Do not put suggestions for external macros.
if pat.span.from_expansion() {
return;
}

let arg_span = arg.span.source_callsite();

let ty = cx.typeck_results().expr_ty(arg);
Expand Down Expand Up @@ -77,6 +82,8 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
};

let sub = if let Some(recv) = extract_iterator_next_call(cx, arg)
&& recv.span.can_be_used_for_suggestions()
&& recv.span.between(arg_span.shrink_to_hi()).can_be_used_for_suggestions()
&& let Ok(recv_snip) = cx.sess().source_map().span_to_snippet(recv.span)
{
ForLoopsOverFalliblesLoopSub::RemoveNext {
Expand Down
14 changes: 0 additions & 14 deletions tests/crashes/147973.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[macro_export]
macro_rules! identity {
($x:ident) => {
$x
};
}

#[macro_export]
macro_rules! do_loop {
($x:ident) => {
for $crate::identity!($x) in $x {}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ check-pass
//@ aux-build:external-macro-issue-148114.rs

// This test ensures we do not trigger the lint on external macros
// ref. <https://github.com/rust-lang/rust/issues/148114>

#![deny(for_loops_over_fallibles)]

extern crate external_macro_issue_148114 as dep;

fn main() {
let _name = Some(1);
dep::do_loop!(_name);
}
21 changes: 21 additions & 0 deletions tests/ui/lint/for-loops-over-fallibles/macro-iterator-next.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This test ensures that the `for-loops-over-fallibles` lint doesn't suggest
// removing `next`.
// ref. <https://github.com/rust-lang/rust-clippy/issues/16133>

#![forbid(for_loops_over_fallibles)]
//~^ NOTE: the lint level is defined here

fn main() {
macro_rules! mac {
(next $e:expr) => {
$e.iter().next()
};
}

for _ in mac!(next [1, 2]) {}
//~^ ERROR: for loop over an `Option`. This is more readably written as an `if let` statement
//~| NOTE: in this expansion of desugaring of `for` loop
//~| NOTE: in this expansion of desugaring of `for` loop
//~| HELP: to check pattern in a loop use `while let`
//~| HELP: consider using `if let` to clear intent
}
24 changes: 24 additions & 0 deletions tests/ui/lint/for-loops-over-fallibles/macro-iterator-next.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error: for loop over an `Option`. This is more readably written as an `if let` statement
--> $DIR/macro-iterator-next.rs:15:14
|
LL | for _ in mac!(next [1, 2]) {}
| ^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/macro-iterator-next.rs:5:11
|
LL | #![forbid(for_loops_over_fallibles)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: to check pattern in a loop use `while let`
|
LL - for _ in mac!(next [1, 2]) {}
LL + while let Some(_) = mac!(next [1, 2]) {}
|
help: consider using `if let` to clear intent
|
LL - for _ in mac!(next [1, 2]) {}
LL + if let Some(_) = mac!(next [1, 2]) {}
|

error: aborting due to 1 previous error

Loading