Skip to content

Commit e485266

Browse files
committed
Auto merge of #128461 - matthiaskrgr:rollup-3dpp11g, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #123813 (Add `REDUNDANT_IMPORTS` lint for new redundant import detection) - #126697 ([RFC] mbe: consider the `_` in 2024 an expression) - #127159 (match lowering: Hide `Candidate` from outside the lowering algorithm) - #128244 (Peel off explicit (or implicit) deref before suggesting clone on move error in borrowck, remove some hacks) - #128431 (Add myself as VxWorks target maintainer for reference) - #128438 (Add special-case for [T, 0] in dropck_outlives) - #128457 (Fix docs for OnceLock::get_mut_or_init) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 71b2116 + 7060a2f commit e485266

File tree

137 files changed

+1590
-1332
lines changed

Some content is hidden

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

137 files changed

+1590
-1332
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+24-68
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
563563
} = move_spans
564564
&& can_suggest_clone
565565
{
566-
self.suggest_cloning(err, ty, expr, None, Some(move_spans));
566+
self.suggest_cloning(err, ty, expr, Some(move_spans));
567567
} else if self.suggest_hoisting_call_outside_loop(err, expr) && can_suggest_clone {
568568
// The place where the type moves would be misleading to suggest clone.
569569
// #121466
570-
self.suggest_cloning(err, ty, expr, None, Some(move_spans));
570+
self.suggest_cloning(err, ty, expr, Some(move_spans));
571571
}
572572
}
573573

@@ -1229,8 +1229,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
12291229
&self,
12301230
err: &mut Diag<'_>,
12311231
ty: Ty<'tcx>,
1232-
mut expr: &'tcx hir::Expr<'tcx>,
1233-
mut other_expr: Option<&'tcx hir::Expr<'tcx>>,
1232+
expr: &'tcx hir::Expr<'tcx>,
12341233
use_spans: Option<UseSpans<'tcx>>,
12351234
) {
12361235
if let hir::ExprKind::Struct(_, _, Some(_)) = expr.kind {
@@ -1242,66 +1241,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
12421241
return;
12431242
}
12441243

1245-
if let Some(some_other_expr) = other_expr
1246-
&& let Some(parent_binop) =
1247-
self.infcx.tcx.hir().parent_iter(expr.hir_id).find_map(|n| {
1248-
if let (hir_id, hir::Node::Expr(e)) = n
1249-
&& let hir::ExprKind::AssignOp(_binop, target, _arg) = e.kind
1250-
&& target.hir_id == expr.hir_id
1251-
{
1252-
Some(hir_id)
1253-
} else {
1254-
None
1255-
}
1256-
})
1257-
&& let Some(other_parent_binop) =
1258-
self.infcx.tcx.hir().parent_iter(some_other_expr.hir_id).find_map(|n| {
1259-
if let (hir_id, hir::Node::Expr(expr)) = n
1260-
&& let hir::ExprKind::AssignOp(..) = expr.kind
1261-
{
1262-
Some(hir_id)
1263-
} else {
1264-
None
1265-
}
1266-
})
1267-
&& parent_binop == other_parent_binop
1268-
{
1269-
// Explicitly look for `expr += other_expr;` and avoid suggesting
1270-
// `expr.clone() += other_expr;`, instead suggesting `expr += other_expr.clone();`.
1271-
other_expr = Some(expr);
1272-
expr = some_other_expr;
1273-
}
1274-
'outer: {
1275-
if let ty::Ref(..) = ty.kind() {
1276-
// We check for either `let binding = foo(expr, other_expr);` or
1277-
// `foo(expr, other_expr);` and if so we don't suggest an incorrect
1278-
// `foo(expr, other_expr).clone()`
1279-
if let Some(other_expr) = other_expr
1280-
&& let Some(parent_let) =
1281-
self.infcx.tcx.hir().parent_iter(expr.hir_id).find_map(|n| {
1282-
if let (hir_id, hir::Node::LetStmt(_) | hir::Node::Stmt(_)) = n {
1283-
Some(hir_id)
1284-
} else {
1285-
None
1286-
}
1287-
})
1288-
&& let Some(other_parent_let) =
1289-
self.infcx.tcx.hir().parent_iter(other_expr.hir_id).find_map(|n| {
1290-
if let (hir_id, hir::Node::LetStmt(_) | hir::Node::Stmt(_)) = n {
1291-
Some(hir_id)
1292-
} else {
1293-
None
1294-
}
1295-
})
1296-
&& parent_let == other_parent_let
1297-
{
1298-
// Explicitly check that we don't have `foo(&*expr, other_expr)`, as cloning the
1299-
// result of `foo(...)` won't help.
1300-
break 'outer;
1301-
}
1302-
}
1303-
}
1304-
let ty = ty.peel_refs();
13051244
if self.implements_clone(ty) {
13061245
self.suggest_cloning_inner(err, ty, expr);
13071246
} else if let ty::Adt(def, args) = ty.kind()
@@ -1573,10 +1512,27 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
15731512
);
15741513
self.suggest_copy_for_type_in_cloned_ref(&mut err, place);
15751514
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
1576-
if let Some(expr) = self.find_expr(borrow_span)
1577-
&& let Some(ty) = typeck_results.node_type_opt(expr.hir_id)
1578-
{
1579-
self.suggest_cloning(&mut err, ty, expr, self.find_expr(span), Some(move_spans));
1515+
if let Some(expr) = self.find_expr(borrow_span) {
1516+
// This is a borrow span, so we want to suggest cloning the referent.
1517+
if let hir::ExprKind::AddrOf(_, _, borrowed_expr) = expr.kind
1518+
&& let Some(ty) = typeck_results.expr_ty_opt(borrowed_expr)
1519+
{
1520+
self.suggest_cloning(&mut err, ty, borrowed_expr, Some(move_spans));
1521+
} else if typeck_results.expr_adjustments(expr).first().is_some_and(|adj| {
1522+
matches!(
1523+
adj.kind,
1524+
ty::adjustment::Adjust::Borrow(ty::adjustment::AutoBorrow::Ref(
1525+
_,
1526+
ty::adjustment::AutoBorrowMutability::Not
1527+
| ty::adjustment::AutoBorrowMutability::Mut {
1528+
allow_two_phase_borrow: ty::adjustment::AllowTwoPhase::No
1529+
}
1530+
))
1531+
)
1532+
}) && let Some(ty) = typeck_results.expr_ty_opt(expr)
1533+
{
1534+
self.suggest_cloning(&mut err, ty, expr, Some(move_spans));
1535+
}
15801536
}
15811537
self.buffer_error(err);
15821538
}

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
564564

565565
fn add_move_hints(&self, error: GroupedMoveError<'tcx>, err: &mut Diag<'_>, span: Span) {
566566
match error {
567-
GroupedMoveError::MovesFromPlace {
568-
mut binds_to, move_from, span: other_span, ..
569-
} => {
567+
GroupedMoveError::MovesFromPlace { mut binds_to, move_from, .. } => {
570568
self.add_borrow_suggestions(err, span);
571569
if binds_to.is_empty() {
572570
let place_ty = move_from.ty(self.body, self.infcx.tcx).ty;
@@ -576,7 +574,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
576574
};
577575

578576
if let Some(expr) = self.find_expr(span) {
579-
self.suggest_cloning(err, place_ty, expr, self.find_expr(other_span), None);
577+
self.suggest_cloning(err, place_ty, expr, None);
580578
}
581579

582580
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
@@ -608,13 +606,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
608606
};
609607

610608
if let Some(expr) = self.find_expr(use_span) {
611-
self.suggest_cloning(
612-
err,
613-
place_ty,
614-
expr,
615-
self.find_expr(span),
616-
Some(use_spans),
617-
);
609+
self.suggest_cloning(err, place_ty, expr, Some(use_spans));
618610
}
619611

620612
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
@@ -739,7 +731,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
739731
let place_desc = &format!("`{}`", self.local_names[*local].unwrap());
740732

741733
if let Some(expr) = self.find_expr(binding_span) {
742-
self.suggest_cloning(err, bind_to.ty, expr, None, None);
734+
self.suggest_cloning(err, bind_to.ty, expr, None);
743735
}
744736

745737
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {

compiler/rustc_lint/messages.ftl

+4-4
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,10 @@ lint_reason_must_be_string_literal = reason must be a string literal
700700
lint_reason_must_come_last = reason in lint attribute must come last
701701
702702
lint_redundant_import = the item `{$ident}` is imported redundantly
703-
.label_imported_here = the item `{ident}` is already imported here
704-
.label_defined_here = the item `{ident}` is already defined here
705-
.label_imported_prelude = the item `{ident}` is already imported by the extern prelude
706-
.label_defined_prelude = the item `{ident}` is already defined by the extern prelude
703+
.label_imported_here = the item `{$ident}` is already imported here
704+
.label_defined_here = the item `{$ident}` is already defined here
705+
.label_imported_prelude = the item `{$ident}` is already imported by the extern prelude
706+
.label_defined_prelude = the item `{$ident}` is already defined by the extern prelude
707707
708708
lint_redundant_import_visibility = glob import doesn't reexport anything with visibility `{$import_vis}` because no imported item is public enough
709709
.note = the most public imported item is `{$max_vis}`

compiler/rustc_lint_defs/src/builtin.rs

+26
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ declare_lint_pass! {
8282
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
8383
PTR_CAST_ADD_AUTO_TO_OBJECT,
8484
PUB_USE_OF_PRIVATE_EXTERN_CRATE,
85+
REDUNDANT_IMPORTS,
8586
REDUNDANT_LIFETIMES,
8687
REFINING_IMPL_TRAIT_INTERNAL,
8788
REFINING_IMPL_TRAIT_REACHABLE,
@@ -426,6 +427,31 @@ declare_lint! {
426427
"imports that are never used"
427428
}
428429

430+
declare_lint! {
431+
/// The `redundant_imports` lint detects imports that are redundant due to being
432+
/// imported already; either through a previous import, or being present in
433+
/// the prelude.
434+
///
435+
/// ### Example
436+
///
437+
/// ```rust,compile_fail
438+
/// #![deny(redundant_imports)]
439+
/// use std::option::Option::None;
440+
/// fn foo() -> Option<i32> { None }
441+
/// ```
442+
///
443+
/// {{produces}}
444+
///
445+
/// ### Explanation
446+
///
447+
/// Redundant imports are unnecessary and can be removed to simplify code.
448+
/// If you intended to re-export the item to make it available outside of the
449+
/// module, add a visibility modifier like `pub`.
450+
pub REDUNDANT_IMPORTS,
451+
Allow,
452+
"imports that are redundant due to being imported already"
453+
}
454+
429455
declare_lint! {
430456
/// The `must_not_suspend` lint guards against values that shouldn't be held across suspend points
431457
/// (`.await`)

0 commit comments

Comments
 (0)