Skip to content

Rollup of 8 pull requests #125436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4be041a
Also apply `warn(for_loops_over_fallibles)` to &T and &mut T, not jus…
zachs18 May 15, 2024
7d7eb97
Fix new for_loops_over_fallibles hits in compiler.
zachs18 May 15, 2024
77f288c
Include reference in lint diagnostic
zachs18 May 15, 2024
ea549fd
Add tests for 'Also apply `warn(for_loops_over_fallibles)` to &T and …
zachs18 May 15, 2024
0e46759
Fix more new for_loops_over_fallibles hits in compiler.
zachs18 May 15, 2024
66573b7
Use 'a' article for &Option.
zachs18 May 15, 2024
6b818dd
Fix article in test
zachs18 May 15, 2024
376a8c0
Allow for_loops_over_fallibles in test that tests &mut Result as Into…
zachs18 May 15, 2024
a59d071
Fix `for_loops_over_fallibles` hits in compiletest/src/json.rs
zachs18 May 16, 2024
3a21fb5
Wrap Context.ext in AssertUnwindSafe
workingjubilee May 22, 2024
8fde7e3
For OutsideLoop we should not suggest add 'block label in if block, o…
surechen Apr 8, 2024
5da41f5
self-contained linker: retry without -fuse-ld=lld on older GCCs
lqd May 22, 2024
c8844df
Clarify the meaning of the span within `mbe::TokenTree::MetaVar`.
nnethercote May 19, 2024
3fc8f89
Clarify `parse` a little.
nnethercote May 19, 2024
b6de782
Clarify a comment.
nnethercote May 16, 2024
c679a55
Convert some `token_joint_hidden` calls to `token_joint`.
nnethercote May 16, 2024
a1b6d46
Use `JointHidden` in a couple of suitable places.
nnethercote May 16, 2024
4d513cb
Add some comments.
nnethercote May 20, 2024
07b7cd6
Add some tests for public-private dependencies.
ehuss Mar 17, 2024
5f4424b
Handle `ReVar` in `note_and_explain_region`.
nnethercote May 12, 2024
ddb81ce
rewrite and rename issue-46239
Oneirical May 17, 2024
5ee8267
Rollup merge of #122665 - ehuss:pub-priv-tests, r=davidtwco
matthiaskrgr May 23, 2024
96134e1
Rollup merge of #123623 - surechen:fix_123261, r=estebank
matthiaskrgr May 23, 2024
72fd85c
Rollup merge of #125054 - nnethercote:fix-124973, r=compiler-errors
matthiaskrgr May 23, 2024
4af1c31
Rollup merge of #125156 - zachs18:for_loops_over_fallibles_behind_ref…
matthiaskrgr May 23, 2024
f131ee6
Rollup merge of #125222 - Oneirical:fifth, r=jieyouxu
matthiaskrgr May 23, 2024
3c79f0c
Rollup merge of #125316 - nnethercote:tweak-Spacing, r=petrochenkov
matthiaskrgr May 23, 2024
5126d4b
Rollup merge of #125392 - workingjubilee:unwind-a-problem-in-context,…
matthiaskrgr May 23, 2024
748647f
Rollup merge of #125417 - lqd:lld-retry, r=petrochenkov
matthiaskrgr May 23, 2024
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
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::deriving::generic::*;
use crate::errors;
use core::ops::ControlFlow;
use rustc_ast as ast;
use rustc_ast::visit::walk_list;
use rustc_ast::visit::visit_opt;
use rustc_ast::{attr, EnumDef, VariantData};
use rustc_expand::base::{Annotatable, DummyResult, ExtCtxt};
use rustc_span::symbol::Ident;
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<'a, 'b> rustc_ast::visit::Visitor<'a> for DetectNonVariantDefaultAttr<'a, '
self.visit_ident(v.ident);
self.visit_vis(&v.vis);
self.visit_variant_data(&v.data);
walk_list!(self, visit_anon_const, &v.disr_expr);
visit_opt!(self, visit_anon_const, &v.disr_expr);
for attr in &v.attrs {
rustc_ast::visit::walk_attribute(self, attr);
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/borrow_check.html

use rustc_ast::visit::walk_list;
use rustc_ast::visit::visit_opt;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -168,7 +168,7 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h
hir::StmtKind::Expr(..) | hir::StmtKind::Semi(..) => visitor.visit_stmt(statement),
}
}
walk_list!(visitor, visit_expr, &blk.expr);
visit_opt!(visitor, visit_expr, &blk.expr);
}

visitor.cx = prev_cx;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ lint_extern_without_abi = extern declarations without an explicit ABI are deprec
.help = the default ABI is {$default_abi}

lint_for_loops_over_fallibles =
for loop over {$article} `{$ty}`. This is more readably written as an `if let` statement
for loop over {$article} `{$ref_prefix}{$ty}`. This is more readably written as an `if let` statement
.suggestion = consider using `if let` to clear intent
.remove_next = to iterate over `{$recv_snip}` remove the call to `next`
.use_while_let = to check pattern in a loop use `while let`
Expand Down
17 changes: 15 additions & 2 deletions compiler/rustc_lint/src/for_loops_over_fallibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,27 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {

let ty = cx.typeck_results().expr_ty(arg);

let &ty::Adt(adt, args) = ty.kind() else { return };
let (adt, args, ref_mutability) = match ty.kind() {
&ty::Adt(adt, args) => (adt, args, None),
&ty::Ref(_, ty, mutability) => match ty.kind() {
&ty::Adt(adt, args) => (adt, args, Some(mutability)),
_ => return,
},
_ => return,
};

let (article, ty, var) = match adt.did() {
did if cx.tcx.is_diagnostic_item(sym::Option, did) && ref_mutability.is_some() => ("a", "Option", "Some"),
did if cx.tcx.is_diagnostic_item(sym::Option, did) => ("an", "Option", "Some"),
did if cx.tcx.is_diagnostic_item(sym::Result, did) => ("a", "Result", "Ok"),
_ => return,
};

let ref_prefix = match ref_mutability {
None => "",
Some(ref_mutability) => ref_mutability.ref_prefix_str(),
};

let sub = if let Some(recv) = extract_iterator_next_call(cx, arg)
&& let Ok(recv_snip) = cx.sess().source_map().span_to_snippet(recv.span)
{
Expand All @@ -85,7 +98,7 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
cx.emit_span_lint(
FOR_LOOPS_OVER_FALLIBLES,
arg.span,
ForLoopsOverFalliblesDiag { article, ty, sub, question_mark, suggestion },
ForLoopsOverFalliblesDiag { article, ref_prefix, ty, sub, question_mark, suggestion },
);
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ pub enum PtrNullChecksDiag<'a> {
#[diag(lint_for_loops_over_fallibles)]
pub struct ForLoopsOverFalliblesDiag<'a> {
pub article: &'static str,
pub ref_prefix: &'static str,
pub ty: &'static str,
#[subdiagnostic]
pub sub: ForLoopsOverFalliblesLoopSub<'a>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
for subpattern in prefix.iter() {
self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
}
for subpattern in slice {
if let Some(subpattern) = slice {
self.visit_primary_bindings(
subpattern,
pattern_user_ty.clone().subslice(from, to),
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{Module, ModuleOrUniformRoot, NameBinding, ParentScope, PathResult};
use crate::{ResolutionError, Resolver, Segment, UseError};

use rustc_ast::ptr::P;
use rustc_ast::visit::{walk_list, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
use rustc_ast::visit::{visit_opt, walk_list, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
use rustc_ast::*;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_errors::{codes::*, Applicability, DiagArgValue, IntoDiagArg, StashKey};
Expand Down Expand Up @@ -3280,7 +3280,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
fn resolve_local(&mut self, local: &'ast Local) {
debug!("resolving local ({:?})", local);
// Resolve the type.
walk_list!(self, visit_ty, &local.ty);
visit_opt!(self, visit_ty, &local.ty);

// Resolve the initializer.
if let Some((init, els)) = local.kind.init_else_opt() {
Expand Down Expand Up @@ -3479,8 +3479,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
fn resolve_arm(&mut self, arm: &'ast Arm) {
self.with_rib(ValueNS, RibKind::Normal, |this| {
this.resolve_pattern_top(&arm.pat, PatternSource::Match);
walk_list!(this, visit_expr, &arm.guard);
walk_list!(this, visit_expr, &arm.body);
visit_opt!(this, visit_expr, &arm.guard);
visit_opt!(this, visit_expr, &arm.body);
});
}

Expand Down
1 change: 1 addition & 0 deletions library/core/tests/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub fn test_iter() {
}

#[test]
#[allow(for_loops_over_fallibles)]
pub fn test_iter_mut() {
let mut ok: Result<isize, &'static str> = Ok(100);
for loc in ok.iter_mut() {
Expand Down
4 changes: 2 additions & 2 deletions src/tools/compiletest/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ fn push_expected_errors(

// Add notes for the backtrace
for span in primary_spans {
for frame in &span.expansion {
if let Some(frame) = &span.expansion {
push_backtrace(expected_errors, frame, file_name);
}
}
Expand Down Expand Up @@ -315,7 +315,7 @@ fn push_backtrace(
});
}

for previous_expansion in &expansion.span.expansion {
if let Some(previous_expansion) = &expansion.span.expansion {
push_backtrace(expected_errors, previous_expansion, file_name);
}
}
22 changes: 22 additions & 0 deletions tests/ui/lint/for_loop_over_fallibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,25 @@ fn _returns_result() -> Result<(), ()> {

Ok(())
}

fn _by_ref() {
// Shared refs
for _ in &Some(1) {}
//~^ WARN for loop over a `&Option`. This is more readably written as an `if let` statement
//~| HELP to check pattern in a loop use `while let`
//~| HELP consider using `if let` to clear intent
for _ in &Ok::<_, ()>(1) {}
//~^ WARN for loop over a `&Result`. This is more readably written as an `if let` statement
//~| HELP to check pattern in a loop use `while let`
//~| HELP consider using `if let` to clear intent

// Mutable refs
for _ in &mut Some(1) {}
//~^ WARN for loop over a `&mut Option`. This is more readably written as an `if let` statement
//~| HELP to check pattern in a loop use `while let`
//~| HELP consider using `if let` to clear intent
for _ in &mut Ok::<_, ()>(1) {}
//~^ WARN for loop over a `&mut Result`. This is more readably written as an `if let` statement
//~| HELP to check pattern in a loop use `while let`
//~| HELP consider using `if let` to clear intent
}
62 changes: 61 additions & 1 deletion tests/ui/lint/for_loop_over_fallibles.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,65 @@ help: consider using `if let` to clear intent
LL | if let Ok(_) = Ok::<_, ()>([0; 0]) {}
| ~~~~~~~~~~ ~~~

warning: 6 warnings emitted
warning: for loop over a `&Option`. This is more readably written as an `if let` statement
--> $DIR/for_loop_over_fallibles.rs:47:14
|
LL | for _ in &Some(1) {}
| ^^^^^^^^
|
help: to check pattern in a loop use `while let`
|
LL | while let Some(_) = &Some(1) {}
| ~~~~~~~~~~~~~~~ ~~~
help: consider using `if let` to clear intent
|
LL | if let Some(_) = &Some(1) {}
| ~~~~~~~~~~~~ ~~~

warning: for loop over a `&Result`. This is more readably written as an `if let` statement
--> $DIR/for_loop_over_fallibles.rs:51:14
|
LL | for _ in &Ok::<_, ()>(1) {}
| ^^^^^^^^^^^^^^^
|
help: to check pattern in a loop use `while let`
|
LL | while let Ok(_) = &Ok::<_, ()>(1) {}
| ~~~~~~~~~~~~~ ~~~
help: consider using `if let` to clear intent
|
LL | if let Ok(_) = &Ok::<_, ()>(1) {}
| ~~~~~~~~~~ ~~~

warning: for loop over a `&mut Option`. This is more readably written as an `if let` statement
--> $DIR/for_loop_over_fallibles.rs:57:14
|
LL | for _ in &mut Some(1) {}
| ^^^^^^^^^^^^
|
help: to check pattern in a loop use `while let`
|
LL | while let Some(_) = &mut Some(1) {}
| ~~~~~~~~~~~~~~~ ~~~
help: consider using `if let` to clear intent
|
LL | if let Some(_) = &mut Some(1) {}
| ~~~~~~~~~~~~ ~~~

warning: for loop over a `&mut Result`. This is more readably written as an `if let` statement
--> $DIR/for_loop_over_fallibles.rs:61:14
|
LL | for _ in &mut Ok::<_, ()>(1) {}
| ^^^^^^^^^^^^^^^^^^^
|
help: to check pattern in a loop use `while let`
|
LL | while let Ok(_) = &mut Ok::<_, ()>(1) {}
| ~~~~~~~~~~~~~ ~~~
help: consider using `if let` to clear intent
|
LL | if let Ok(_) = &mut Ok::<_, ()>(1) {}
| ~~~~~~~~~~ ~~~

warning: 10 warnings emitted