Skip to content

Commit 27749e5

Browse files
committed
Rebase and CI, please just work 🙏
1 parent 8663c6f commit 27749e5

File tree

14 files changed

+158
-148
lines changed

14 files changed

+158
-148
lines changed

clippy_lints/src/borrow_pats/body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<'a, 'tcx> BodyAnalysis<'a, 'tcx> {
170170
impl<'a, 'tcx> Visitor<'tcx> for BodyAnalysis<'a, 'tcx> {
171171
fn visit_assign(&mut self, target: &Place<'tcx>, rval: &Rvalue<'tcx>, _loc: mir::Location) {
172172
match rval {
173-
Rvalue::Ref(_reg, BorrowKind::Fake, _src) => {
173+
Rvalue::Ref(_reg, BorrowKind::Fake(_), _src) => {
174174
#[allow(clippy::needless_return)]
175175
return;
176176
},

clippy_lints/src/borrow_pats/info.rs

+23-35
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_index::IndexVec;
99
use rustc_lint::LateContext;
1010
use rustc_middle::mir;
1111
use rustc_middle::mir::{BasicBlock, Local, Place};
12-
use rustc_middle::ty::TyCtxt;
12+
use rustc_middle::ty::{self, Ty, TyCtxt};
1313
use rustc_span::Symbol;
1414
use smallvec::SmallVec;
1515

@@ -24,14 +24,9 @@ pub struct AnalysisInfo<'tcx> {
2424
pub tcx: TyCtxt<'tcx>,
2525
pub body: &'tcx mir::Body<'tcx>,
2626
pub def_id: LocalDefId,
27-
// borrow_set: Rc<borrowck::BorrowSet<'tcx>>,
28-
// locs: FxIndexMap<Location, Vec<BorrowIndex>>
2927
pub cfg: IndexVec<BasicBlock, CfgInfo>,
3028
/// The set defines the loop bbs, and the basic block determines the end of the loop
3129
pub terms: FxHashMap<BasicBlock, FxHashMap<Local, Vec<Local>>>,
32-
/// The final block that contains the return.
33-
pub return_block: BasicBlock,
34-
// FIXME: This should be a IndexVec
3530
pub locals: IndexVec<Local, LocalInfo<'tcx>>,
3631
pub preds: IndexVec<BasicBlock, SmallVec<[BasicBlock; 1]>>,
3732
pub preds_unlooped: IndexVec<BasicBlock, SmallVec<[BasicBlock; 1]>>,
@@ -69,7 +64,6 @@ impl<'tcx> AnalysisInfo<'tcx> {
6964
let MetaAnalysis {
7065
cfg,
7166
terms,
72-
return_block,
7367
locals,
7468
preds,
7569
preds_unlooped,
@@ -88,7 +82,6 @@ impl<'tcx> AnalysisInfo<'tcx> {
8882
def_id,
8983
cfg,
9084
terms,
91-
return_block,
9285
locals,
9386
preds,
9487
preds_unlooped,
@@ -246,44 +239,39 @@ pub enum SimpleTyKind {
246239
}
247240

248241
impl SimpleTyKind {
249-
pub fn from_ty(ty: rustc_middle::ty::Ty<'_>) -> Self {
242+
pub fn from_ty(ty: Ty<'_>) -> Self {
250243
match ty.kind() {
251-
rustc_middle::ty::TyKind::Tuple(tys) if tys.is_empty() => SimpleTyKind::Unit,
252-
rustc_middle::ty::TyKind::Tuple(_) => SimpleTyKind::Tuple,
244+
ty::Tuple(tys) if tys.is_empty() => SimpleTyKind::Unit,
245+
ty::Tuple(_) => SimpleTyKind::Tuple,
253246

254-
rustc_middle::ty::TyKind::Never => SimpleTyKind::Never,
247+
ty::Never => SimpleTyKind::Never,
255248

256-
rustc_middle::ty::TyKind::Bool
257-
| rustc_middle::ty::TyKind::Char
258-
| rustc_middle::ty::TyKind::Int(_)
259-
| rustc_middle::ty::TyKind::Uint(_)
260-
| rustc_middle::ty::TyKind::Float(_)
261-
| rustc_middle::ty::TyKind::Str => SimpleTyKind::Primitive,
249+
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => SimpleTyKind::Primitive,
262250

263-
rustc_middle::ty::TyKind::Adt(_, _) => SimpleTyKind::UserDef,
251+
ty::Adt(_, _) => SimpleTyKind::UserDef,
264252

265-
rustc_middle::ty::TyKind::Array(_, _) | rustc_middle::ty::TyKind::Slice(_) => SimpleTyKind::Sequence,
253+
ty::Array(_, _) | ty::Slice(_) => SimpleTyKind::Sequence,
266254

267-
rustc_middle::ty::TyKind::Ref(_, _, _) => SimpleTyKind::Reference,
255+
ty::Ref(_, _, _) => SimpleTyKind::Reference,
268256

269-
rustc_middle::ty::TyKind::Foreign(_) => SimpleTyKind::Foreign,
270-
rustc_middle::ty::TyKind::RawPtr(_) => SimpleTyKind::RawPtr,
257+
ty::Foreign(_) => SimpleTyKind::Foreign,
258+
ty::RawPtr(_, _) => SimpleTyKind::RawPtr,
271259

272-
rustc_middle::ty::TyKind::FnDef(_, _) | rustc_middle::ty::TyKind::FnPtr(_) => SimpleTyKind::Fn,
273-
rustc_middle::ty::TyKind::Closure(_, _) => SimpleTyKind::Closure,
260+
ty::FnDef(_, _) | ty::FnPtr(_) => SimpleTyKind::Fn,
261+
ty::Closure(_, _) => SimpleTyKind::Closure,
274262

275-
rustc_middle::ty::TyKind::Alias(rustc_middle::ty::AliasKind::Opaque, _)
276-
| rustc_middle::ty::TyKind::Dynamic(_, _, _) => SimpleTyKind::TraitObj,
263+
ty::Alias(ty::AliasKind::Opaque, _) | ty::Dynamic(_, _, _) => SimpleTyKind::TraitObj,
277264

278-
rustc_middle::ty::TyKind::Param(_) => SimpleTyKind::Generic,
279-
rustc_middle::ty::TyKind::Bound(_, _) | rustc_middle::ty::TyKind::Alias(_, _) => SimpleTyKind::Idk,
265+
ty::Param(_) => SimpleTyKind::Generic,
266+
ty::Bound(_, _) | ty::Alias(_, _) => SimpleTyKind::Idk,
280267

281-
rustc_middle::ty::TyKind::CoroutineClosure(_, _)
282-
| rustc_middle::ty::TyKind::Coroutine(_, _)
283-
| rustc_middle::ty::TyKind::CoroutineWitness(_, _)
284-
| rustc_middle::ty::TyKind::Placeholder(_)
285-
| rustc_middle::ty::TyKind::Infer(_)
286-
| rustc_middle::ty::TyKind::Error(_) => unreachable!("{ty:#?}"),
268+
ty::CoroutineClosure(_, _)
269+
| ty::Coroutine(_, _)
270+
| ty::CoroutineWitness(_, _)
271+
| ty::Placeholder(_)
272+
| ty::Infer(_)
273+
| ty::Error(_)
274+
| ty::Pat(_, _) => unreachable!("{ty:#?}"),
287275
}
288276
}
289277
}

clippy_lints/src/borrow_pats/info/meta.rs

+32-24
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,20 @@ impl<'a, 'tcx> MetaAnalysis<'a, 'tcx> {
9494
| mir::TerminatorKind::Assert { target, .. }
9595
| mir::TerminatorKind::Call { target: Some(target), .. }
9696
| mir::TerminatorKind::Drop { target, .. }
97-
| mir::TerminatorKind::InlineAsm { destination: Some(target), .. }
9897
| mir::TerminatorKind::Goto { target } => {
9998
self.preds[*target].push(bb);
10099
CfgInfo::Linear(*target)
101100
},
101+
mir::TerminatorKind::InlineAsm { targets, .. } => {
102+
let mut branches = SmallVec::new();
103+
branches.extend(targets.iter().copied());
104+
105+
for target in &branches {
106+
self.preds[*target].push(bb);
107+
}
108+
109+
CfgInfo::Condition { branches }
110+
},
102111
mir::TerminatorKind::SwitchInt { targets, .. } => {
103112
let mut branches = SmallVec::new();
104113
branches.extend_from_slice(targets.all_targets());
@@ -114,8 +123,7 @@ impl<'a, 'tcx> MetaAnalysis<'a, 'tcx> {
114123
| mir::TerminatorKind::UnwindTerminate(_)
115124
| mir::TerminatorKind::Unreachable
116125
| mir::TerminatorKind::CoroutineDrop
117-
| mir::TerminatorKind::Call { .. }
118-
| mir::TerminatorKind::InlineAsm { .. } => {
126+
| mir::TerminatorKind::Call { .. } => {
119127
CfgInfo::None
120128
},
121129
mir::TerminatorKind::Return => {
@@ -129,31 +137,31 @@ impl<'a, 'tcx> MetaAnalysis<'a, 'tcx> {
129137
}
130138

131139
fn visit_terminator_for_terms(&mut self, term: &Terminator<'tcx>, bb: BasicBlock) {
132-
if let
133-
mir::TerminatorKind::Call {
134-
func,
135-
args,
136-
destination,
137-
..
138-
} = &term.kind {
139-
assert!(destination.projection.is_empty());
140-
let dest = destination.local;
141-
self.terms.insert(
142-
bb,
143-
calc_call_local_relations(self.tcx.0, self.body, func, dest, args, &mut self.stats),
144-
);
145-
}
140+
if let mir::TerminatorKind::Call {
141+
func,
142+
args,
143+
destination,
144+
..
145+
} = &term.kind
146+
{
147+
assert!(destination.projection.is_empty());
148+
let dest = destination.local;
149+
self.terms.insert(
150+
bb,
151+
calc_call_local_relations(self.tcx.0, self.body, func, dest, args, &mut self.stats),
152+
);
153+
}
146154
}
147155

148156
fn visit_terminator_for_locals(&mut self, term: &Terminator<'tcx>, _bb: BasicBlock) {
149157
if let mir::TerminatorKind::Call { destination, .. } = &term.kind {
150-
// TODO: Should mut arguments be handled?
151-
assert!(destination.projection.is_empty());
152-
let local = destination.local;
153-
self.locals
154-
.get_mut(local)
155-
.unwrap()
156-
.add_assign(*destination, DataInfo::Computed);
158+
// TODO: Should mut arguments be handled?
159+
assert!(destination.projection.is_empty());
160+
let local = destination.local;
161+
self.locals
162+
.get_mut(local)
163+
.unwrap()
164+
.add_assign(*destination, DataInfo::Computed);
157165
}
158166
}
159167
}

clippy_lints/src/borrow_pats/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#![expect(unused)]
2-
#![allow(clippy::module_name_repetitions, clippy::default_trait_access, clippy::wildcard_imports ,clippy::enum_variant_names)]
2+
#![allow(
3+
clippy::module_name_repetitions,
4+
clippy::default_trait_access,
5+
clippy::wildcard_imports,
6+
clippy::enum_variant_names
7+
)]
38
//! # TODOs
49
//! - [ ] Update meta analysis
510
//! - [ ] Handle loops by partially retraverse them
@@ -88,11 +93,11 @@ declare_clippy_lint! {
8893
/// ### Why is this bad?
8994
///
9095
/// ### Example
91-
/// ```no_run
96+
/// ```ignore
9297
/// // example code where clippy issues a warning
9398
/// ```
9499
/// Use instead:
95-
/// ```no_run
100+
/// ```ignore
96101
/// // example code which does not raise clippy warning
97102
/// ```
98103
#[clippy::version = "1.78.0"]

clippy_lints/src/borrow_pats/owned.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,22 @@ pub enum OwnedPat {
128128
PartArgBorrowMut,
129129
PartArgBorrowMutExtended,
130130
/// Two temp borrows might alias each other, for example like this:
131-
/// ```
131+
/// ```ignore
132132
/// take_2(&self.field, &self.field);
133133
/// ```
134134
/// This also includes fields and sub fields
135-
/// ```
135+
/// ```ignore
136136
/// take_2(&self.field, &self.field.sub_field);
137137
/// ```
138138
AliasedBorrow,
139139
/// A function takes mutliple `&mut` references to different parts of the object
140-
/// ```
140+
/// ```ignore
141141
/// take_2(&mut self.field_a, &mut self.field_b);
142142
/// ```
143143
/// Mutable borrows can't be aliased.
144144
MultipleMutBorrowsInArgs,
145145
/// A function takes both a mutable and an immutable loan as the function input.
146-
/// ```
146+
/// ```ignore
147147
/// take_2(&self.field_a, &mut self.field_b);
148148
/// ```
149149
/// The places can not be aliased.
@@ -160,7 +160,7 @@ pub enum OwnedPat {
160160
/// This value is involved in a two phased borrow. Meaning that an argument is calculated
161161
/// using the value itself. Example:
162162
///
163-
/// ```
163+
/// ```ignore
164164
/// fn two_phase_borrow_1(mut vec: Vec<usize>) {
165165
/// vec.push(vec.len());
166166
/// }
@@ -192,7 +192,7 @@ pub enum OwnedPat {
192192
TwoPhasedBorrow,
193193
/// A value is first mutably initilized and then moved into an unmut value.
194194
///
195-
/// ```
195+
/// ```ignore
196196
/// fn mut_and_shadow_immut() {
197197
/// let mut x = "Hello World".to_string();
198198
/// x.push('x');
@@ -220,7 +220,7 @@ pub enum OwnedPat {
220220
PartOwningAnonDrop,
221221
/// This value is being dropped (by rustc) early to be replaced.
222222
///
223-
/// ```
223+
/// ```ignore
224224
/// let data = String::new();
225225
///
226226
/// // Rustc will first drop the old value of `data`
@@ -270,7 +270,7 @@ impl<'a, 'tcx> Visitor<'tcx> for OwnedAnalysis<'a, 'tcx> {
270270
}
271271

272272
fn visit_assign(&mut self, target: &Place<'tcx>, rvalue: &Rvalue<'tcx>, loc: Location) {
273-
if let Rvalue::Ref(_region, BorrowKind::Fake, _place) = &rvalue {
273+
if let Rvalue::Ref(_region, BorrowKind::Fake(_), _place) = &rvalue {
274274
return;
275275
}
276276

@@ -438,6 +438,7 @@ impl<'a, 'tcx> OwnedAnalysis<'a, 'tcx> {
438438
}
439439
},
440440
mir::AggregateKind::Coroutine(_, _) | mir::AggregateKind::CoroutineClosure(_, _) => unreachable!(),
441+
mir::AggregateKind::RawPtr(_, _) => {},
441442
}
442443
}
443444
}
@@ -608,6 +609,7 @@ impl<'a, 'tcx> OwnedAnalysis<'a, 'tcx> {
608609
self.pats.insert(OwnedPat::PartMovedToClosure);
609610
}
610611
},
612+
mir::AggregateKind::RawPtr(_, _) => {},
611613
mir::AggregateKind::Coroutine(_, _) | mir::AggregateKind::CoroutineClosure(_, _) => unreachable!(),
612614
}
613615
}

clippy_lints/src/borrow_pats/owned/state.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct StateInfo<'tcx> {
2222
///
2323
/// **Note 1**: Named borrows can be created in two ways (Because of course
2424
/// they can...)
25-
/// ```
25+
/// ```ignore
2626
/// // From: `mut_named_ref_non_kill`
2727
/// // let mut x = 1;
2828
/// // let mut p: &u32 = &x;
@@ -38,7 +38,7 @@ pub struct StateInfo<'tcx> {
3838
/// **Note 2**: Correction there are three ways to created named borrows...
3939
/// Not sure why but let's take `mut_named_ref_non_kill` as and example for `y`
4040
///
41-
/// ```
41+
/// ```ignore
4242
/// // y => _2
4343
/// // named => _3
4444
/// _8 = &_2
@@ -95,7 +95,7 @@ pub struct BorrowInfo<'tcx> {
9595
/// The place that is being borrowed
9696
pub broker: Place<'tcx>,
9797
/// This is the mutability of the original borrow. If we have a double borrow, like this:
98-
/// ```
98+
/// ```ignore
9999
/// let mut data = String::new();
100100
///
101101
/// // Loan 1

clippy_lints/src/borrow_pats/rustc_extention.rs

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ impl PlaceMagic for mir::Place<'_> {
7474
}
7575

7676
pub trait LocalMagic {
77+
#[expect(clippy::wrong_self_convention)]
7778
fn as_place(self) -> Place<'static>;
7879
}
7980

clippy_lints/src/borrow_pats/stats.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl CrateStats {
113113
/// owned value and `_2` is the named references, they could have the following
114114
/// shapes:
115115
///
116-
/// ```
116+
/// ```ignore
117117
/// // Direct
118118
/// _2 = &_1
119119
///
@@ -178,7 +178,7 @@ pub struct OwnedStats {
178178
/// Temp borrows are used for function calls.
179179
///
180180
/// The MIR commonly looks like this:
181-
/// ```
181+
/// ```ignore
182182
/// _3 = &_1
183183
/// _4 = &(*_3)
184184
/// _2 = function(move _4)

clippy_lints/src/borrow_pats/util.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#![warn(unused)]
2-
use std::collections::{BTreeMap, BTreeSet};
32

43
use clippy_utils::ty::{for_each_param_ty, for_each_ref_region, for_each_region};
54
use rustc_ast::Mutability;
65
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
76
use rustc_hir::def_id::{DefId, LocalDefId};
87
use rustc_middle::mir::{Body, Local, Operand, Place};
9-
use rustc_middle::ty::{FnSig, GenericArgsRef, GenericPredicates, Region, Ty, TyCtxt, TyKind};
8+
use rustc_middle::ty::{self, FnSig, GenericArgsRef, GenericPredicates, Region, Ty, TyCtxt};
109
use rustc_span::source_map::Spanned;
1110

1211
use crate::borrow_pats::{LocalMagic, PlaceMagic};
@@ -36,7 +35,7 @@ struct FuncReals<'tcx> {
3635
/// A list of several universes
3736
///
3837
/// Mapping from `'short` (key) is outlives by `'long` (value)
39-
multiverse: BTreeMap<Region<'tcx>, BTreeSet<Region<'tcx>>>,
38+
multiverse: FxHashMap<Region<'tcx>, FxHashSet<Region<'tcx>>>,
4039
sig: FnSig<'tcx>,
4140
args: GenericArgsRef<'tcx>,
4241
/// Indicates that a possibly returned value has generics with `'ReErased`
@@ -150,7 +149,7 @@ impl<'tcx> FuncReals<'tcx> {
150149
/// This function takes an operand, that identifies a function and returns the
151150
/// indices of the arguments that might be parents of the return type.
152151
///
153-
/// ```
152+
/// ```ignore
154153
/// fn example<'c, 'a: 'c, 'b: 'c>(cond: bool, a: &'a u32, b: &'b u32) -> &'c u32 {
155154
/// # todo!()
156155
/// }
@@ -217,7 +216,7 @@ pub fn calc_call_local_relations<'tcx>(
217216
builder = FuncReals::from_fn_def(tcx, def_id, generic_args);
218217
} else if let Some(place) = func.place() {
219218
let local_ty = body.local_decls[place.local].ty;
220-
if let TyKind::FnDef(def_id, generic_args) = local_ty.kind() {
219+
if let ty::FnDef(def_id, generic_args) = local_ty.kind() {
221220
builder = FuncReals::from_fn_def(tcx, *def_id, generic_args);
222221
} else {
223222
stats.arg_relation_possibly_missed_due_to_late_bounds += 1;

0 commit comments

Comments
 (0)