Skip to content

Commit 7cf247b

Browse files
committed
Fix various lints
1 parent 8a0a9cc commit 7cf247b

File tree

12 files changed

+113
-154
lines changed

12 files changed

+113
-154
lines changed

crates/rustc_utils/src/cache.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ where
7979
/// # Panics
8080
///
8181
/// If this is a recursive invocation for this key.
82-
pub fn get(&self, key: In, compute: impl FnOnce(In) -> Out) -> &Out {
82+
pub fn get(&self, key: &In, compute: impl FnOnce(In) -> Out) -> &Out {
8383
self
8484
.get_maybe_recursive(key, compute)
8585
.unwrap_or_else(recursion_panic)
@@ -90,10 +90,10 @@ where
9090
/// Returns `None` if this is a recursive invocation of `get` for key `key`.
9191
pub fn get_maybe_recursive<'a>(
9292
&'a self,
93-
key: In,
93+
key: &In,
9494
compute: impl FnOnce(In) -> Out,
9595
) -> Option<&'a Out> {
96-
if !self.0.borrow().contains_key(&key) {
96+
if !self.0.borrow().contains_key(key) {
9797
self.0.borrow_mut().insert(key.clone(), None);
9898
let out = Box::pin(compute(key.clone()));
9999
self.0.borrow_mut().insert(key.clone(), Some(out));
@@ -102,7 +102,7 @@ where
102102
let cache = self.0.borrow();
103103
// Important here to first `unwrap` the `Option` created by `get`, then
104104
// propagate the potential option stored in the map.
105-
let entry = cache.get(&key).expect("invariant broken").as_ref()?;
105+
let entry = cache.get(key).expect("invariant broken").as_ref()?;
106106

107107
// SAFETY: because the entry is pinned, it cannot move and this pointer will
108108
// only be invalidated if Cache is dropped. The returned reference has a lifetime
@@ -139,7 +139,7 @@ where
139139
/// # Panics
140140
///
141141
/// If this is a recursive invocation for this key.
142-
pub fn get(&self, key: In, compute: impl FnOnce(In) -> Out) -> Out {
142+
pub fn get(&self, key: &In, compute: impl FnOnce(In) -> Out) -> Out {
143143
self
144144
.get_maybe_recursive(key, compute)
145145
.unwrap_or_else(recursion_panic)
@@ -151,16 +151,16 @@ where
151151
/// Returns `None` if this is a recursive invocation of `get` for key `key`.
152152
pub fn get_maybe_recursive(
153153
&self,
154-
key: In,
154+
key: &In,
155155
compute: impl FnOnce(In) -> Out,
156156
) -> Option<Out> {
157-
if !self.0.borrow().contains_key(&key) {
157+
if !self.0.borrow().contains_key(key) {
158158
self.0.borrow_mut().insert(key.clone(), None);
159159
let out = compute(key.clone());
160160
self.0.borrow_mut().insert(key.clone(), Some(out));
161161
}
162162

163-
*self.0.borrow_mut().get(&key).expect("invariant broken")
163+
*self.0.borrow_mut().get(key).expect("invariant broken")
164164
}
165165
}
166166

crates/rustc_utils/src/hir/ty.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Utilities for [`Ty`].
22
3-
use rustc_data_structures::captures::Captures;
43
use rustc_hir::def_id::DefId;
54
use rustc_infer::infer::TyCtxtInferExt;
65
use rustc_middle::ty::{GenericArgKind, ParamEnv, Region, Ty, TyCtxt, TypingEnv};
@@ -9,57 +8,49 @@ use rustc_type_ir::TypingMode;
98

109
/// Extension trait for [`Ty`].
1110
pub trait TyExt<'tcx> {
12-
type AllRegionsIter<'a>: Iterator<Item = Region<'tcx>>
13-
where
14-
Self: 'a;
15-
1611
/// Returns an iterator over the regions appearing within a type.
17-
fn inner_regions(&self) -> Self::AllRegionsIter<'_>;
12+
fn inner_regions(self) -> impl Iterator<Item = Region<'tcx>>;
1813

1914
/// Returns true if a type implements a given trait.
2015
fn does_implement_trait(
21-
&self,
16+
self,
2217
tcx: TyCtxt<'tcx>,
2318
param_env: ParamEnv<'tcx>,
2419
trait_def_id: DefId,
2520
) -> bool;
2621

22+
#[allow(clippy::wrong_self_convention)]
2723
/// Returns true if a type implements `Copy`.
28-
fn is_copyable(&self, tcx: TyCtxt<'tcx>, typing_env: TypingEnv<'tcx>) -> bool;
24+
fn is_copyable(self, tcx: TyCtxt<'tcx>, typing_env: TypingEnv<'tcx>) -> bool;
2925
}
3026

3127
impl<'tcx> TyExt<'tcx> for Ty<'tcx> {
32-
type AllRegionsIter<'a>
33-
= impl Iterator<Item = Region<'tcx>> + Captures<'tcx> + 'a
34-
where
35-
Self: 'a;
36-
37-
fn inner_regions(&self) -> Self::AllRegionsIter<'_> {
28+
fn inner_regions(self) -> impl Iterator<Item = Region<'tcx>> {
3829
self.walk().filter_map(|part| match part.unpack() {
3930
GenericArgKind::Lifetime(region) => Some(region),
4031
_ => None,
4132
})
4233
}
4334

4435
fn does_implement_trait(
45-
&self,
36+
self,
4637
tcx: TyCtxt<'tcx>,
4738
param_env: ParamEnv<'tcx>,
4839
trait_def_id: DefId,
4940
) -> bool {
5041
use rustc_infer::traits::EvaluationResult;
5142

5243
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
53-
let ty = tcx.erase_regions(*self);
44+
let ty = tcx.erase_regions(self);
5445
let result = infcx.type_implements_trait(trait_def_id, [ty], param_env);
5546
matches!(
5647
result,
5748
EvaluationResult::EvaluatedToOk | EvaluationResult::EvaluatedToOkModuloRegions
5849
)
5950
}
6051

61-
fn is_copyable(&self, tcx: TyCtxt<'tcx>, typing_env: TypingEnv<'tcx>) -> bool {
62-
let ty = tcx.erase_regions(*self);
52+
fn is_copyable(self, tcx: TyCtxt<'tcx>, typing_env: TypingEnv<'tcx>) -> bool {
53+
let ty = tcx.erase_regions(self);
6354
ty.is_copy_modulo_regions(tcx, typing_env)
6455
}
6556
}

crates/rustc_utils/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,24 @@
2121
impl_trait_in_assoc_type,
2222
doc_auto_cfg, // for feature gates in documentation
2323
)]
24-
#![allow(clippy::len_zero, clippy::len_without_is_empty)]
24+
#![warn(clippy::pedantic)]
25+
#![allow(
26+
clippy::len_zero,
27+
clippy::len_without_is_empty,
28+
clippy::must_use_candidate,
29+
clippy::return_self_not_must_use,
30+
clippy::missing_panics_doc,
31+
clippy::missing_errors_doc,
32+
clippy::doc_markdown,
33+
clippy::single_match_else,
34+
clippy::if_not_else,
35+
clippy::match_on_vec_items,
36+
clippy::map_unwrap_or,
37+
clippy::match_wildcard_for_single_variants,
38+
clippy::items_after_statements,
39+
clippy::implicit_hasher,
40+
clippy::wildcard_imports
41+
)]
2542

2643
extern crate either;
2744
extern crate rustc_borrowck;
@@ -62,7 +79,7 @@ pub use crate::{
6279
source_map::span::{SpanDataExt, SpanExt},
6380
};
6481

65-
/// Utility for hashset literals. Same as maplit::hashset but works with FxHasher.
82+
/// Utility for hashset literals. Same as [`maplit::hashset`] but works with [`FxHasher`].
6683
#[macro_export]
6784
macro_rules! hashset {
6885
(@single $($x:tt)*) => (());

crates/rustc_utils/src/mir/adt_def.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,21 @@ use rustc_middle::ty::{AdtDef, FieldDef, TyCtxt};
55

66
/// Extension trait for [`AdtDef`].
77
pub trait AdtDefExt<'tcx> {
8-
type AllVisibleFieldsIter: Iterator<Item = &'tcx FieldDef>;
9-
108
/// Returns an iterator over all the fields of the ADT that are visible
119
/// from `module`.
1210
fn all_visible_fields(
1311
self,
1412
module: DefId,
1513
tcx: TyCtxt<'tcx>,
16-
) -> Self::AllVisibleFieldsIter;
14+
) -> impl Iterator<Item = &'tcx FieldDef>;
1715
}
1816

1917
impl<'tcx> AdtDefExt<'tcx> for AdtDef<'tcx> {
20-
type AllVisibleFieldsIter = impl Iterator<Item = &'tcx FieldDef>;
2118
fn all_visible_fields(
2219
self,
2320
module: DefId,
2421
tcx: TyCtxt<'tcx>,
25-
) -> Self::AllVisibleFieldsIter {
22+
) -> impl Iterator<Item = &'tcx FieldDef> {
2623
self
2724
.all_fields()
2825
.filter(move |field| field.vis.is_accessible_from(module, tcx))

crates/rustc_utils/src/mir/body.rs

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ use std::{
88

99
use anyhow::{ensure, Result};
1010
use pretty::PrettyPrintMirOptions;
11-
use rustc_data_structures::{captures::Captures, fx::FxHashMap as HashMap};
11+
use rustc_data_structures::fx::FxHashMap as HashMap;
1212
use rustc_hir::{def_id::DefId, CoroutineDesugaring, CoroutineKind, HirId};
1313
use rustc_middle::{
14-
mir::{pretty::write_mir_fn, *},
14+
mir::{
15+
pretty, pretty::write_mir_fn, BasicBlock, Body, Local, Location, Place, SourceInfo,
16+
TerminatorKind, VarDebugInfoContents,
17+
},
1518
ty::{Region, Ty, TyCtxt},
1619
};
1720
use smallvec::SmallVec;
@@ -21,24 +24,14 @@ use crate::{PlaceExt, TyExt};
2124

2225
/// Extension trait for [`Body`].
2326
pub trait BodyExt<'tcx> {
24-
type AllReturnsIter<'a>: Iterator<Item = Location>
25-
where
26-
Self: 'a;
27-
2827
/// Returns an iterator over the locations of [`TerminatorKind::Return`] instructions in a body.
29-
fn all_returns(&self) -> Self::AllReturnsIter<'_>;
30-
31-
type AllLocationsIter<'a>: Iterator<Item = Location>
32-
where
33-
Self: 'a;
28+
fn all_returns(&self) -> impl Iterator<Item = Location> + '_;
3429

3530
/// Returns an iterator over all the locations in a body.
36-
fn all_locations(&self) -> Self::AllLocationsIter<'_>;
37-
38-
type LocationsIter: Iterator<Item = Location>;
31+
fn all_locations(&self) -> impl Iterator<Item = Location> + '_;
3932

4033
/// Returns all the locations in a [`BasicBlock`].
41-
fn locations_in_block(&self, block: BasicBlock) -> Self::LocationsIter;
34+
fn locations_in_block(&self, block: BasicBlock) -> impl Iterator<Item = Location>;
4235

4336
/// Returns a mapping from source-level variable names to [`Local`]s.
4437
fn debug_info_name_map(&self) -> HashMap<String, Local>;
@@ -64,32 +57,22 @@ pub trait BodyExt<'tcx> {
6457
/// locals across await calls.
6558
fn async_context(&self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Ty<'tcx>>;
6659

67-
type PlacesIter<'a>: Iterator<Item = Place<'tcx>>
68-
where
69-
Self: 'a;
70-
7160
/// Returns an iterator over all projections of all local variables in the body.
72-
fn all_places(&self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Self::PlacesIter<'_>;
73-
74-
type ArgRegionsIter<'a>: Iterator<Item = Region<'tcx>>
75-
where
76-
Self: 'a;
61+
fn all_places(
62+
&self,
63+
tcx: TyCtxt<'tcx>,
64+
def_id: DefId,
65+
) -> impl Iterator<Item = Place<'tcx>> + '_;
7766

7867
/// Returns an iterator over all the regions that appear in argument types to the body.
79-
fn regions_in_args(&self) -> Self::ArgRegionsIter<'_>;
80-
81-
type ReturnRegionsIter: Iterator<Item = Region<'tcx>>;
68+
fn regions_in_args(&self) -> impl Iterator<Item = Region<'tcx>> + '_;
8269

8370
/// Returns an iterator over all the regions that appear in the body's return type.
84-
fn regions_in_return(&self) -> Self::ReturnRegionsIter;
71+
fn regions_in_return(&self) -> impl Iterator<Item = Region<'tcx>> + '_;
8572
}
8673

8774
impl<'tcx> BodyExt<'tcx> for Body<'tcx> {
88-
type AllReturnsIter<'a>
89-
= impl Iterator<Item = Location> + Captures<'tcx> + 'a
90-
where
91-
Self: 'a;
92-
fn all_returns(&self) -> Self::AllReturnsIter<'_> {
75+
fn all_returns(&self) -> impl Iterator<Item = Location> + '_ {
9376
self
9477
.basic_blocks
9578
.iter_enumerated()
@@ -102,24 +85,19 @@ impl<'tcx> BodyExt<'tcx> for Body<'tcx> {
10285
})
10386
}
10487

105-
type AllLocationsIter<'a>
106-
= impl Iterator<Item = Location> + Captures<'tcx> + 'a
107-
where
108-
Self: 'a;
109-
fn all_locations(&self) -> Self::AllLocationsIter<'_> {
88+
fn all_locations(&self) -> impl Iterator<Item = Location> + '_ {
11089
self
11190
.basic_blocks
11291
.iter_enumerated()
11392
.flat_map(|(block, data)| {
114-
(0 .. data.statements.len() + 1).map(move |statement_index| Location {
93+
(0 ..= data.statements.len()).map(move |statement_index| Location {
11594
block,
11695
statement_index,
11796
})
11897
})
11998
}
12099

121-
type LocationsIter = impl Iterator<Item = Location>;
122-
fn locations_in_block(&self, block: BasicBlock) -> Self::LocationsIter {
100+
fn locations_in_block(&self, block: BasicBlock) -> impl Iterator<Item = Location> {
123101
let num_stmts = self.basic_blocks[block].statements.len();
124102
(0 ..= num_stmts).map(move |statement_index| Location {
125103
block,
@@ -181,46 +159,38 @@ impl<'tcx> BodyExt<'tcx> for Body<'tcx> {
181159
}
182160
}
183161

184-
type ArgRegionsIter<'a>
185-
= impl Iterator<Item = Region<'tcx>> + Captures<'tcx> + 'a
186-
where
187-
Self: 'a;
188-
189-
type ReturnRegionsIter = impl Iterator<Item = Region<'tcx>>;
190-
191-
type PlacesIter<'a>
192-
= impl Iterator<Item = Place<'tcx>> + Captures<'tcx> + 'a
193-
where
194-
Self: 'a;
195-
196-
fn regions_in_args(&self) -> Self::ArgRegionsIter<'_> {
162+
fn regions_in_args(&self) -> impl Iterator<Item = Region<'tcx>> + '_ {
197163
self
198164
.args_iter()
199165
.flat_map(|arg_local| self.local_decls[arg_local].ty.inner_regions())
200166
}
201167

202-
fn regions_in_return(&self) -> Self::ReturnRegionsIter {
168+
fn regions_in_return(&self) -> impl Iterator<Item = Region<'tcx>> + '_ {
203169
self
204170
.return_ty()
205171
.inner_regions()
206172
.collect::<SmallVec<[Region<'tcx>; 8]>>()
207173
.into_iter()
208174
}
209175

210-
fn all_places(&self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Self::PlacesIter<'_> {
176+
fn all_places(
177+
&self,
178+
tcx: TyCtxt<'tcx>,
179+
def_id: DefId,
180+
) -> impl Iterator<Item = Place<'tcx>> + '_ {
211181
self.local_decls.indices().flat_map(move |local| {
212182
Place::from_local(local, tcx).interior_paths(tcx, self, def_id)
213183
})
214184
}
215185
}
216186

217-
pub fn run_dot(path: &Path, buf: Vec<u8>) -> Result<()> {
187+
pub fn run_dot(path: &Path, buf: &[u8]) -> Result<()> {
218188
let mut p = Command::new("dot")
219189
.args(["-Tpdf", "-o", &path.display().to_string()])
220190
.stdin(Stdio::piped())
221191
.spawn()?;
222192

223-
p.stdin.as_mut().unwrap().write_all(&buf)?;
193+
p.stdin.as_mut().unwrap().write_all(buf)?;
224194

225195
let status = p.wait()?;
226196
ensure!(status.success(), "dot for {} failed", path.display());

crates/rustc_utils/src/mir/borrowck_facts.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ pub fn simplify_mir(body: &mut Body<'_>) {
3939
TerminatorKind::FalseEdge { real_target, .. } => TerminatorKind::Goto {
4040
target: real_target,
4141
},
42-
TerminatorKind::FalseUnwind { real_target, .. } => TerminatorKind::Goto {
43-
target: real_target,
44-
},
4542
// Ensures that control dependencies can determine the independence of differnet
4643
// return paths
4744
TerminatorKind::Goto { target } if return_blocks.contains(&target) => {
@@ -90,7 +87,7 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &BorrowCheckResult<'_> {
9087
let body_with_facts: BodyWithBorrowckFacts<'static> =
9188
unsafe { std::mem::transmute(body_with_facts) };
9289
MIR_BODIES.with(|cache| {
93-
cache.get(def_id, |_| body_with_facts);
90+
cache.get(&def_id, |_| body_with_facts);
9491
});
9592

9693
let mut providers = Providers::default();
@@ -117,7 +114,7 @@ pub fn get_body_with_borrowck_facts<'tcx>(
117114
) -> &'tcx BodyWithBorrowckFacts<'tcx> {
118115
let _ = tcx.mir_borrowck(def_id);
119116
MIR_BODIES.with(|cache| {
120-
let body = cache.get(def_id, |_| panic!("mir_borrowck override should have stored body for item: {def_id:?}. Are you sure you registered borrowck_facts::override_queries?"));
117+
let body = cache.get(&def_id, |_| panic!("mir_borrowck override should have stored body for item: {def_id:?}. Are you sure you registered borrowck_facts::override_queries?"));
121118
unsafe {
122119
std::mem::transmute::<
123120
&BodyWithBorrowckFacts<'static>,

0 commit comments

Comments
 (0)