Skip to content

Commit aabe0bd

Browse files
committed
Auto merge of rust-lang#130963 - nyurik:opts-rustc, r=<try>
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.
2 parents b6576e3 + 340bd2a commit aabe0bd

File tree

32 files changed

+144
-116
lines changed

32 files changed

+144
-116
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
198198
if let Some(def_id) = static_def_id {
199199
let path = self.lower_qpath(
200200
sym.id,
201-
&sym.qself,
201+
sym.qself.as_ref(),
202202
&sym.path,
203203
ParamMode::Optional,
204204
AllowReturnTypeNotation::No,

compiler/rustc_ast_lowering/src/delegation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
337337
} else {
338338
let path = self.lower_qpath(
339339
delegation.id,
340-
&delegation.qself,
340+
delegation.qself.as_ref(),
341341
&delegation.path,
342342
ParamMode::Optional,
343343
AllowReturnTypeNotation::No,

compiler/rustc_ast_lowering/src/expr.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
278278
ExprKind::Path(qself, path) => {
279279
let qpath = self.lower_qpath(
280280
e.id,
281-
qself,
281+
qself.as_ref(),
282282
path,
283283
ParamMode::Optional,
284284
AllowReturnTypeNotation::No,
@@ -326,7 +326,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
326326
hir::ExprKind::Struct(
327327
self.arena.alloc(self.lower_qpath(
328328
e.id,
329-
&se.qself,
329+
se.qself.as_ref(),
330330
&se.path,
331331
ParamMode::Optional,
332332
AllowReturnTypeNotation::No,
@@ -1287,7 +1287,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12871287
);
12881288
let qpath = self.lower_qpath(
12891289
callee.id,
1290-
qself,
1290+
qself.as_ref(),
12911291
path,
12921292
ParamMode::Optional,
12931293
AllowReturnTypeNotation::No,
@@ -1308,7 +1308,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13081308
if let Some((qself, path)) = self.extract_unit_struct_path(lhs) {
13091309
let qpath = self.lower_qpath(
13101310
lhs.id,
1311-
qself,
1311+
qself.as_ref(),
13121312
path,
13131313
ParamMode::Optional,
13141314
AllowReturnTypeNotation::No,
@@ -1334,7 +1334,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13341334
}));
13351335
let qpath = self.lower_qpath(
13361336
lhs.id,
1337-
&se.qself,
1337+
se.qself.as_ref(),
13381338
&se.path,
13391339
ParamMode::Optional,
13401340
AllowReturnTypeNotation::No,

compiler/rustc_ast_lowering/src/format.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ fn make_argument<'hir>(
283283
fn make_count<'hir>(
284284
ctx: &mut LoweringContext<'_, 'hir>,
285285
sp: Span,
286-
count: &Option<FormatCount>,
286+
count: Option<&FormatCount>,
287287
argmap: &mut FxIndexMap<(usize, ArgumentType), Option<Span>>,
288288
) -> hir::Expr<'hir> {
289289
match count {
@@ -378,8 +378,8 @@ fn make_format_spec<'hir>(
378378
| ((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
379379
| ((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
380380
let flags = ctx.expr_u32(sp, flags);
381-
let precision = make_count(ctx, sp, precision, argmap);
382-
let width = make_count(ctx, sp, width, argmap);
381+
let precision = make_count(ctx, sp, precision.as_ref(), argmap);
382+
let width = make_count(ctx, sp, width.as_ref(), argmap);
383383
let format_placeholder_new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
384384
sp,
385385
hir::LangItem::FormatPlaceholder,

compiler/rustc_ast_lowering/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12061206
fn lower_path_ty(
12071207
&mut self,
12081208
t: &Ty,
1209-
qself: &Option<ptr::P<QSelf>>,
1209+
qself: Option<&ptr::P<QSelf>>,
12101210
path: &Path,
12111211
param_mode: ParamMode,
12121212
itctx: ImplTraitContext,
@@ -1340,7 +1340,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13401340
return self.lower_ty_direct(ty, itctx);
13411341
}
13421342
TyKind::Path(qself, path) => {
1343-
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
1343+
return self.lower_path_ty(t, qself.as_ref(), path, ParamMode::Explicit, itctx);
13441344
}
13451345
TyKind::ImplicitSelf => {
13461346
let hir_id = self.next_id();
@@ -2222,7 +2222,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22222222
) -> hir::TraitRef<'hir> {
22232223
let path = match self.lower_qpath(
22242224
p.ref_id,
2225-
&None,
2225+
None,
22262226
&p.path,
22272227
ParamMode::Explicit,
22282228
AllowReturnTypeNotation::No,
@@ -2361,7 +2361,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23612361
Res::Def(DefKind::ConstParam, _) => {
23622362
let qpath = self.lower_qpath(
23632363
ty_id,
2364-
&None,
2364+
None,
23652365
path,
23662366
ParamMode::Optional,
23672367
AllowReturnTypeNotation::No,
@@ -2440,7 +2440,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24402440
{
24412441
let qpath = self.lower_qpath(
24422442
expr.id,
2443-
qself,
2443+
qself.as_ref(),
24442444
path,
24452445
ParamMode::Optional,
24462446
AllowReturnTypeNotation::No,

compiler/rustc_ast_lowering/src/pat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
3535
PatKind::TupleStruct(qself, path, pats) => {
3636
let qpath = self.lower_qpath(
3737
pattern.id,
38-
qself,
38+
qself.as_ref(),
3939
path,
4040
ParamMode::Optional,
4141
AllowReturnTypeNotation::No,
@@ -53,7 +53,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
5353
PatKind::Path(qself, path) => {
5454
let qpath = self.lower_qpath(
5555
pattern.id,
56-
qself,
56+
qself.as_ref(),
5757
path,
5858
ParamMode::Optional,
5959
AllowReturnTypeNotation::No,
@@ -65,7 +65,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
6565
PatKind::Struct(qself, path, fields, etc) => {
6666
let qpath = self.lower_qpath(
6767
pattern.id,
68-
qself,
68+
qself.as_ref(),
6969
path,
7070
ParamMode::Optional,
7171
AllowReturnTypeNotation::No,

compiler/rustc_ast_lowering/src/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2525
pub(crate) fn lower_qpath(
2626
&mut self,
2727
id: NodeId,
28-
qself: &Option<ptr::P<QSelf>>,
28+
qself: Option<&ptr::P<QSelf>>,
2929
p: &Path,
3030
param_mode: ParamMode,
3131
allow_return_type_notation: AllowReturnTypeNotation,

compiler/rustc_ast_pretty/src/pprust/state.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1107,9 +1107,9 @@ impl<'a> State<'a> {
11071107
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e, FixupContext::default()), |e| e.span)
11081108
}
11091109

1110-
pub fn print_opt_lifetime(&mut self, lifetime: &Option<ast::Lifetime>) {
1111-
if let Some(lt) = *lifetime {
1112-
self.print_lifetime(lt);
1110+
pub fn print_opt_lifetime(&mut self, lifetime: Option<&ast::Lifetime>) {
1111+
if let Some(lt) = lifetime {
1112+
self.print_lifetime(*lt);
11131113
self.nbsp();
11141114
}
11151115
}
@@ -1160,7 +1160,7 @@ impl<'a> State<'a> {
11601160
}
11611161
ast::TyKind::Ref(lifetime, mt) => {
11621162
self.word("&");
1163-
self.print_opt_lifetime(lifetime);
1163+
self.print_opt_lifetime(lifetime.as_ref());
11641164
self.print_mt(mt, false);
11651165
}
11661166
ast::TyKind::Never => {
@@ -1709,7 +1709,7 @@ impl<'a> State<'a> {
17091709
}
17101710
SelfKind::Region(lt, m) => {
17111711
self.word("&");
1712-
self.print_opt_lifetime(lt);
1712+
self.print_opt_lifetime(lt.as_ref());
17131713
self.print_mutability(*m, false);
17141714
self.word("self")
17151715
}

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a> State<'a> {
151151

152152
fn print_expr_struct(
153153
&mut self,
154-
qself: &Option<P<ast::QSelf>>,
154+
qself: Option<&P<ast::QSelf>>,
155155
path: &ast::Path,
156156
fields: &[ast::ExprField],
157157
rest: &ast::StructRest,
@@ -388,7 +388,7 @@ impl<'a> State<'a> {
388388
self.print_expr_repeat(element, count);
389389
}
390390
ast::ExprKind::Struct(se) => {
391-
self.print_expr_struct(&se.qself, &se.path, &se.fields, &se.rest);
391+
self.print_expr_struct(se.qself.as_ref(), &se.path, &se.fields, &se.rest);
392392
}
393393
ast::ExprKind::Tup(exprs) => {
394394
self.print_expr_tup(exprs);

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -386,18 +386,18 @@ impl<'a> State<'a> {
386386
ast::ItemKind::Delegation(deleg) => self.print_delegation(
387387
&item.attrs,
388388
&item.vis,
389-
&deleg.qself,
389+
deleg.qself.as_ref(),
390390
&deleg.path,
391391
DelegationKind::Single,
392-
&deleg.body,
392+
deleg.body.as_ref(),
393393
),
394394
ast::ItemKind::DelegationMac(deleg) => self.print_delegation(
395395
&item.attrs,
396396
&item.vis,
397-
&deleg.qself,
397+
deleg.qself.as_ref(),
398398
&deleg.prefix,
399399
deleg.suffixes.as_ref().map_or(DelegationKind::Glob, |s| DelegationKind::List(s)),
400-
&deleg.body,
400+
deleg.body.as_ref(),
401401
),
402402
}
403403
self.ann.post(self, AnnNode::Item(item))
@@ -578,18 +578,18 @@ impl<'a> State<'a> {
578578
ast::AssocItemKind::Delegation(deleg) => self.print_delegation(
579579
&item.attrs,
580580
vis,
581-
&deleg.qself,
581+
deleg.qself.as_ref(),
582582
&deleg.path,
583583
DelegationKind::Single,
584-
&deleg.body,
584+
deleg.body.as_ref(),
585585
),
586586
ast::AssocItemKind::DelegationMac(deleg) => self.print_delegation(
587587
&item.attrs,
588588
vis,
589-
&deleg.qself,
589+
deleg.qself.as_ref(),
590590
&deleg.prefix,
591591
deleg.suffixes.as_ref().map_or(DelegationKind::Glob, |s| DelegationKind::List(s)),
592-
&deleg.body,
592+
deleg.body.as_ref(),
593593
),
594594
}
595595
self.ann.post(self, AnnNode::SubItem(id))
@@ -599,10 +599,10 @@ impl<'a> State<'a> {
599599
&mut self,
600600
attrs: &[ast::Attribute],
601601
vis: &ast::Visibility,
602-
qself: &Option<P<ast::QSelf>>,
602+
qself: Option<&P<ast::QSelf>>,
603603
path: &ast::Path,
604604
kind: DelegationKind<'_>,
605-
body: &Option<P<ast::Block>>,
605+
body: Option<&P<ast::Block>>,
606606
) {
607607
if body.is_some() {
608608
self.head("");

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -2921,7 +2921,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
29212921
span,
29222922
..
29232923
},
2924-
) => self.report_escaping_data(borrow_span, &name, upvar_span, upvar_name, span),
2924+
) => self.report_escaping_data(
2925+
borrow_span,
2926+
name.as_deref(),
2927+
upvar_span,
2928+
upvar_name,
2929+
span,
2930+
),
29252931
(Some(name), explanation) => self.report_local_value_does_not_live_long_enough(
29262932
location,
29272933
&name,
@@ -2973,7 +2979,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
29732979
borrow_span,
29742980
span,
29752981
category,
2976-
opt_place_desc.as_ref(),
2982+
opt_place_desc.as_deref(),
29772983
) {
29782984
return diag;
29792985
}
@@ -3315,7 +3321,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
33153321
borrow_span: Span,
33163322
return_span: Span,
33173323
category: ConstraintCategory<'tcx>,
3318-
opt_place_desc: Option<&String>,
3324+
opt_place_desc: Option<&str>,
33193325
) -> Result<(), Diag<'infcx>> {
33203326
let return_kind = match category {
33213327
ConstraintCategory::Return(_) => "return",
@@ -3517,7 +3523,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
35173523
fn report_escaping_data(
35183524
&self,
35193525
borrow_span: Span,
3520-
name: &Option<String>,
3526+
name: Option<&str>,
35213527
upvar_span: Span,
35223528
upvar_name: Symbol,
35233529
escape_span: Span,

compiler/rustc_borrowck/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,15 @@ fn do_mir_borrowck<'tcx>(
228228

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

233233
// We also have a `#[rustc_regions]` annotation that causes us to dump
234234
// information.
235235
nll::dump_annotation(
236236
&infcx,
237237
body,
238238
&regioncx,
239-
&opt_closure_req,
239+
opt_closure_req.as_ref(),
240240
&opaque_type_values,
241241
&mut diags,
242242
);

compiler/rustc_borrowck/src/nll.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ pub(super) fn dump_nll_mir<'tcx>(
224224
infcx: &BorrowckInferCtxt<'tcx>,
225225
body: &Body<'tcx>,
226226
regioncx: &RegionInferenceContext<'tcx>,
227-
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
227+
closure_region_requirements: Option<&ClosureRegionRequirements<'tcx>>,
228228
borrow_set: &BorrowSet<'tcx>,
229229
) {
230230
let tcx = infcx.tcx;
@@ -305,7 +305,7 @@ pub(super) fn dump_annotation<'tcx, 'infcx>(
305305
infcx: &'infcx BorrowckInferCtxt<'tcx>,
306306
body: &Body<'tcx>,
307307
regioncx: &RegionInferenceContext<'tcx>,
308-
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
308+
closure_region_requirements: Option<&ClosureRegionRequirements<'tcx>>,
309309
opaque_type_values: &FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>>,
310310
diags: &mut crate::diags::BorrowckDiags<'infcx, 'tcx>,
311311
) {

compiler/rustc_codegen_gcc/build_system/src/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn check_exit_status(
9292
Err(error)
9393
}
9494

95-
fn command_error<D: Debug>(input: &[&dyn AsRef<OsStr>], cwd: &Option<&Path>, error: D) -> String {
95+
fn command_error<D: Debug>(input: &[&dyn AsRef<OsStr>], cwd: Option<&Path>, error: D) -> String {
9696
format!(
9797
"Command `{}`{} failed to run: {error:?}",
9898
input.iter().map(|s| s.as_ref().to_str().unwrap()).collect::<Vec<_>>().join(" "),

compiler/rustc_codegen_gcc/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
378378
gcc_func,
379379
args.into(),
380380
&func_name,
381-
original_function_name,
381+
original_function_name.map(String::as_str),
382382
)
383383
};
384384
let args_adjusted = args.len() != previous_arg_count;

0 commit comments

Comments
 (0)