Skip to content

Commit

Permalink
Auto merge of rust-lang#130963 - nyurik:opts-rustc, r=<try>
Browse files Browse the repository at this point in the history
Migrate compiler's `&Option<T>` into `Option<&T>`

Similar to rust-lang#130962 but for the compiler.

Trying out my new lint rust-lang/rust-clippy#13336 - according to the [video](https://www.youtube.com/watch?v=6c7pZYP_iIE), this could lead to some performance and memory optimizations.
  • Loading branch information
bors committed Sep 28, 2024
2 parents b6576e3 + 340bd2a commit aabe0bd
Show file tree
Hide file tree
Showing 32 changed files with 144 additions and 116 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
if let Some(def_id) = static_def_id {
let path = self.lower_qpath(
sym.id,
&sym.qself,
sym.qself.as_ref(),
&sym.path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
} else {
let path = self.lower_qpath(
delegation.id,
&delegation.qself,
delegation.qself.as_ref(),
&delegation.path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ExprKind::Path(qself, path) => {
let qpath = self.lower_qpath(
e.id,
qself,
qself.as_ref(),
path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand Down Expand Up @@ -326,7 +326,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ExprKind::Struct(
self.arena.alloc(self.lower_qpath(
e.id,
&se.qself,
se.qself.as_ref(),
&se.path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand Down Expand Up @@ -1287,7 +1287,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
);
let qpath = self.lower_qpath(
callee.id,
qself,
qself.as_ref(),
path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand All @@ -1308,7 +1308,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
if let Some((qself, path)) = self.extract_unit_struct_path(lhs) {
let qpath = self.lower_qpath(
lhs.id,
qself,
qself.as_ref(),
path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand All @@ -1334,7 +1334,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}));
let qpath = self.lower_qpath(
lhs.id,
&se.qself,
se.qself.as_ref(),
&se.path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ fn make_argument<'hir>(
fn make_count<'hir>(
ctx: &mut LoweringContext<'_, 'hir>,
sp: Span,
count: &Option<FormatCount>,
count: Option<&FormatCount>,
argmap: &mut FxIndexMap<(usize, ArgumentType), Option<Span>>,
) -> hir::Expr<'hir> {
match count {
Expand Down Expand Up @@ -378,8 +378,8 @@ fn make_format_spec<'hir>(
| ((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
| ((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
let flags = ctx.expr_u32(sp, flags);
let precision = make_count(ctx, sp, precision, argmap);
let width = make_count(ctx, sp, width, argmap);
let precision = make_count(ctx, sp, precision.as_ref(), argmap);
let width = make_count(ctx, sp, width.as_ref(), argmap);
let format_placeholder_new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
sp,
hir::LangItem::FormatPlaceholder,
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_path_ty(
&mut self,
t: &Ty,
qself: &Option<ptr::P<QSelf>>,
qself: Option<&ptr::P<QSelf>>,
path: &Path,
param_mode: ParamMode,
itctx: ImplTraitContext,
Expand Down Expand Up @@ -1340,7 +1340,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
return self.lower_ty_direct(ty, itctx);
}
TyKind::Path(qself, path) => {
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
return self.lower_path_ty(t, qself.as_ref(), path, ParamMode::Explicit, itctx);
}
TyKind::ImplicitSelf => {
let hir_id = self.next_id();
Expand Down Expand Up @@ -2222,7 +2222,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
) -> hir::TraitRef<'hir> {
let path = match self.lower_qpath(
p.ref_id,
&None,
None,
&p.path,
ParamMode::Explicit,
AllowReturnTypeNotation::No,
Expand Down Expand Up @@ -2361,7 +2361,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
Res::Def(DefKind::ConstParam, _) => {
let qpath = self.lower_qpath(
ty_id,
&None,
None,
path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand Down Expand Up @@ -2440,7 +2440,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
{
let qpath = self.lower_qpath(
expr.id,
qself,
qself.as_ref(),
path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
PatKind::TupleStruct(qself, path, pats) => {
let qpath = self.lower_qpath(
pattern.id,
qself,
qself.as_ref(),
path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand All @@ -53,7 +53,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
PatKind::Path(qself, path) => {
let qpath = self.lower_qpath(
pattern.id,
qself,
qself.as_ref(),
path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand All @@ -65,7 +65,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
PatKind::Struct(qself, path, fields, etc) => {
let qpath = self.lower_qpath(
pattern.id,
qself,
qself.as_ref(),
path,
ParamMode::Optional,
AllowReturnTypeNotation::No,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
pub(crate) fn lower_qpath(
&mut self,
id: NodeId,
qself: &Option<ptr::P<QSelf>>,
qself: Option<&ptr::P<QSelf>>,
p: &Path,
param_mode: ParamMode,
allow_return_type_notation: AllowReturnTypeNotation,
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,9 +1107,9 @@ impl<'a> State<'a> {
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e, FixupContext::default()), |e| e.span)
}

pub fn print_opt_lifetime(&mut self, lifetime: &Option<ast::Lifetime>) {
if let Some(lt) = *lifetime {
self.print_lifetime(lt);
pub fn print_opt_lifetime(&mut self, lifetime: Option<&ast::Lifetime>) {
if let Some(lt) = lifetime {
self.print_lifetime(*lt);
self.nbsp();
}
}
Expand Down Expand Up @@ -1160,7 +1160,7 @@ impl<'a> State<'a> {
}
ast::TyKind::Ref(lifetime, mt) => {
self.word("&");
self.print_opt_lifetime(lifetime);
self.print_opt_lifetime(lifetime.as_ref());
self.print_mt(mt, false);
}
ast::TyKind::Never => {
Expand Down Expand Up @@ -1709,7 +1709,7 @@ impl<'a> State<'a> {
}
SelfKind::Region(lt, m) => {
self.word("&");
self.print_opt_lifetime(lt);
self.print_opt_lifetime(lt.as_ref());
self.print_mutability(*m, false);
self.word("self")
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<'a> State<'a> {

fn print_expr_struct(
&mut self,
qself: &Option<P<ast::QSelf>>,
qself: Option<&P<ast::QSelf>>,
path: &ast::Path,
fields: &[ast::ExprField],
rest: &ast::StructRest,
Expand Down Expand Up @@ -388,7 +388,7 @@ impl<'a> State<'a> {
self.print_expr_repeat(element, count);
}
ast::ExprKind::Struct(se) => {
self.print_expr_struct(&se.qself, &se.path, &se.fields, &se.rest);
self.print_expr_struct(se.qself.as_ref(), &se.path, &se.fields, &se.rest);
}
ast::ExprKind::Tup(exprs) => {
self.print_expr_tup(exprs);
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,18 @@ impl<'a> State<'a> {
ast::ItemKind::Delegation(deleg) => self.print_delegation(
&item.attrs,
&item.vis,
&deleg.qself,
deleg.qself.as_ref(),
&deleg.path,
DelegationKind::Single,
&deleg.body,
deleg.body.as_ref(),
),
ast::ItemKind::DelegationMac(deleg) => self.print_delegation(
&item.attrs,
&item.vis,
&deleg.qself,
deleg.qself.as_ref(),
&deleg.prefix,
deleg.suffixes.as_ref().map_or(DelegationKind::Glob, |s| DelegationKind::List(s)),
&deleg.body,
deleg.body.as_ref(),
),
}
self.ann.post(self, AnnNode::Item(item))
Expand Down Expand Up @@ -578,18 +578,18 @@ impl<'a> State<'a> {
ast::AssocItemKind::Delegation(deleg) => self.print_delegation(
&item.attrs,
vis,
&deleg.qself,
deleg.qself.as_ref(),
&deleg.path,
DelegationKind::Single,
&deleg.body,
deleg.body.as_ref(),
),
ast::AssocItemKind::DelegationMac(deleg) => self.print_delegation(
&item.attrs,
vis,
&deleg.qself,
deleg.qself.as_ref(),
&deleg.prefix,
deleg.suffixes.as_ref().map_or(DelegationKind::Glob, |s| DelegationKind::List(s)),
&deleg.body,
deleg.body.as_ref(),
),
}
self.ann.post(self, AnnNode::SubItem(id))
Expand All @@ -599,10 +599,10 @@ impl<'a> State<'a> {
&mut self,
attrs: &[ast::Attribute],
vis: &ast::Visibility,
qself: &Option<P<ast::QSelf>>,
qself: Option<&P<ast::QSelf>>,
path: &ast::Path,
kind: DelegationKind<'_>,
body: &Option<P<ast::Block>>,
body: Option<&P<ast::Block>>,
) {
if body.is_some() {
self.head("");
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2921,7 +2921,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
span,
..
},
) => self.report_escaping_data(borrow_span, &name, upvar_span, upvar_name, span),
) => self.report_escaping_data(
borrow_span,
name.as_deref(),
upvar_span,
upvar_name,
span,
),
(Some(name), explanation) => self.report_local_value_does_not_live_long_enough(
location,
&name,
Expand Down Expand Up @@ -2973,7 +2979,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
borrow_span,
span,
category,
opt_place_desc.as_ref(),
opt_place_desc.as_deref(),
) {
return diag;
}
Expand Down Expand Up @@ -3315,7 +3321,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
borrow_span: Span,
return_span: Span,
category: ConstraintCategory<'tcx>,
opt_place_desc: Option<&String>,
opt_place_desc: Option<&str>,
) -> Result<(), Diag<'infcx>> {
let return_kind = match category {
ConstraintCategory::Return(_) => "return",
Expand Down Expand Up @@ -3517,7 +3523,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
fn report_escaping_data(
&self,
borrow_span: Span,
name: &Option<String>,
name: Option<&str>,
upvar_span: Span,
upvar_name: Symbol,
escape_span: Span,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,15 @@ fn do_mir_borrowck<'tcx>(

// Dump MIR results into a file, if that is enabled. This let us
// write unit-tests, as well as helping with debugging.
nll::dump_nll_mir(&infcx, body, &regioncx, &opt_closure_req, &borrow_set);
nll::dump_nll_mir(&infcx, body, &regioncx, opt_closure_req.as_ref(), &borrow_set);

// We also have a `#[rustc_regions]` annotation that causes us to dump
// information.
nll::dump_annotation(
&infcx,
body,
&regioncx,
&opt_closure_req,
opt_closure_req.as_ref(),
&opaque_type_values,
&mut diags,
);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub(super) fn dump_nll_mir<'tcx>(
infcx: &BorrowckInferCtxt<'tcx>,
body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
closure_region_requirements: Option<&ClosureRegionRequirements<'tcx>>,
borrow_set: &BorrowSet<'tcx>,
) {
let tcx = infcx.tcx;
Expand Down Expand Up @@ -305,7 +305,7 @@ pub(super) fn dump_annotation<'tcx, 'infcx>(
infcx: &'infcx BorrowckInferCtxt<'tcx>,
body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
closure_region_requirements: Option<&ClosureRegionRequirements<'tcx>>,
opaque_type_values: &FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>>,
diags: &mut crate::diags::BorrowckDiags<'infcx, 'tcx>,
) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/build_system/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn check_exit_status(
Err(error)
}

fn command_error<D: Debug>(input: &[&dyn AsRef<OsStr>], cwd: &Option<&Path>, error: D) -> String {
fn command_error<D: Debug>(input: &[&dyn AsRef<OsStr>], cwd: Option<&Path>, error: D) -> String {
format!(
"Command `{}`{} failed to run: {error:?}",
input.iter().map(|s| s.as_ref().to_str().unwrap()).collect::<Vec<_>>().join(" "),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
gcc_func,
args.into(),
&func_name,
original_function_name,
original_function_name.map(String::as_str),
)
};
let args_adjusted = args.len() != previous_arg_count;
Expand Down
Loading

0 comments on commit aabe0bd

Please sign in to comment.