Skip to content

Stop using Key trait unnecessarily #143441

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 1 commit into from
Jul 6, 2025
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
5 changes: 2 additions & 3 deletions compiler/rustc_const_eval/src/const_eval/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Not in interpret to make sure we do not use private implementation details

use rustc_abi::{FieldIdx, VariantIdx};
use rustc_middle::query::Key;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::{bug, mir};
use rustc_span::DUMMY_SP;
use tracing::instrument;

use crate::interpret::InterpCx;
Expand Down Expand Up @@ -71,8 +71,7 @@ pub fn tag_for_variant_provider<'tcx>(
let (ty, variant_index) = key.value;
assert!(ty.is_enum());

let ecx =
InterpCx::new(tcx, ty.default_span(tcx), key.typing_env, crate::const_eval::DummyMachine);
let ecx = InterpCx::new(tcx, DUMMY_SP, key.typing_env, crate::const_eval::DummyMachine);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this span anyways, and even if we did, it would be nice to use a relevant def span since it's just an enum anyways.


let layout = ecx.layout_of(ty).unwrap();
ecx.tag_for_variant(layout, variant_index).unwrap().map(|(tag, _tag_field)| tag)
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_lint/src/map_unit_fn.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use rustc_hir::{Expr, ExprKind, HirId, Stmt, StmtKind};
use rustc_middle::query::Key;
use rustc_middle::ty::{self, Ty};
use rustc_session::{declare_lint, declare_lint_pass};

Expand Down Expand Up @@ -69,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
.span_of_impl(*id)
.unwrap_or(default_span),
argument_label: args[0].span,
map_label: arg_ty.default_span(cx.tcx),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was DUMMY_SP, but dummy span labels fall back to the primary span coincidentally (lol)

map_label: span,
suggestion: path.ident.span,
replace: "for_each".to_string(),
},
Expand All @@ -88,7 +87,7 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
.span_of_impl(*id)
.unwrap_or(default_span),
argument_label: args[0].span,
map_label: arg_ty.default_span(cx.tcx),
map_label: span,
suggestion: path.ident.span,
replace: "for_each".to_string(),
},
Expand Down
25 changes: 12 additions & 13 deletions tests/ui/lint/lint_map_unit_fn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ LL + x.iter_mut().for_each(foo);
error: `Iterator::map` call that discard the iterator's values
--> $DIR/lint_map_unit_fn.rs:11:18
|
LL | x.iter_mut().map(|items| {
| ^ -------
| | |
| ____________________|___this function returns `()`, which is likely not what you wanted
| | __________________|
| | |
LL | | |
LL | | | items.sort();
LL | | | });
| | | -^ after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items
| | |_____||
| |_______|
| called `Iterator::map` with callable that returns `()`
LL | x.iter_mut().map(|items| {
| ^ -------
| | |
| ___________________|___this function returns `()`, which is likely not what you wanted
| | __________________|
| ||
LL | ||
LL | || items.sort();
LL | || });
| ||_____-^ after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items
| |______|
| called `Iterator::map` with callable that returns `()`
|
= note: `Iterator::map`, like many of the methods on `Iterator`, gets executed lazily, meaning that its effects won't be visible until it is iterated
help: you might have meant to use `Iterator::for_each`
Expand Down
Loading