Skip to content

Commit

Permalink
Rebase and CI, please just work 🙏
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed May 5, 2024
1 parent 8663c6f commit 27749e5
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 148 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/borrow_pats/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl<'a, 'tcx> BodyAnalysis<'a, 'tcx> {
impl<'a, 'tcx> Visitor<'tcx> for BodyAnalysis<'a, 'tcx> {
fn visit_assign(&mut self, target: &Place<'tcx>, rval: &Rvalue<'tcx>, _loc: mir::Location) {
match rval {
Rvalue::Ref(_reg, BorrowKind::Fake, _src) => {
Rvalue::Ref(_reg, BorrowKind::Fake(_), _src) => {
#[allow(clippy::needless_return)]
return;
},
Expand Down
58 changes: 23 additions & 35 deletions clippy_lints/src/borrow_pats/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_index::IndexVec;
use rustc_lint::LateContext;
use rustc_middle::mir;
use rustc_middle::mir::{BasicBlock, Local, Place};
use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Symbol;
use smallvec::SmallVec;

Expand All @@ -24,14 +24,9 @@ pub struct AnalysisInfo<'tcx> {
pub tcx: TyCtxt<'tcx>,
pub body: &'tcx mir::Body<'tcx>,
pub def_id: LocalDefId,
// borrow_set: Rc<borrowck::BorrowSet<'tcx>>,
// locs: FxIndexMap<Location, Vec<BorrowIndex>>
pub cfg: IndexVec<BasicBlock, CfgInfo>,
/// The set defines the loop bbs, and the basic block determines the end of the loop
pub terms: FxHashMap<BasicBlock, FxHashMap<Local, Vec<Local>>>,
/// The final block that contains the return.
pub return_block: BasicBlock,
// FIXME: This should be a IndexVec
pub locals: IndexVec<Local, LocalInfo<'tcx>>,
pub preds: IndexVec<BasicBlock, SmallVec<[BasicBlock; 1]>>,
pub preds_unlooped: IndexVec<BasicBlock, SmallVec<[BasicBlock; 1]>>,
Expand Down Expand Up @@ -69,7 +64,6 @@ impl<'tcx> AnalysisInfo<'tcx> {
let MetaAnalysis {
cfg,
terms,
return_block,
locals,
preds,
preds_unlooped,
Expand All @@ -88,7 +82,6 @@ impl<'tcx> AnalysisInfo<'tcx> {
def_id,
cfg,
terms,
return_block,
locals,
preds,
preds_unlooped,
Expand Down Expand Up @@ -246,44 +239,39 @@ pub enum SimpleTyKind {
}

impl SimpleTyKind {
pub fn from_ty(ty: rustc_middle::ty::Ty<'_>) -> Self {
pub fn from_ty(ty: Ty<'_>) -> Self {
match ty.kind() {
rustc_middle::ty::TyKind::Tuple(tys) if tys.is_empty() => SimpleTyKind::Unit,
rustc_middle::ty::TyKind::Tuple(_) => SimpleTyKind::Tuple,
ty::Tuple(tys) if tys.is_empty() => SimpleTyKind::Unit,
ty::Tuple(_) => SimpleTyKind::Tuple,

rustc_middle::ty::TyKind::Never => SimpleTyKind::Never,
ty::Never => SimpleTyKind::Never,

rustc_middle::ty::TyKind::Bool
| rustc_middle::ty::TyKind::Char
| rustc_middle::ty::TyKind::Int(_)
| rustc_middle::ty::TyKind::Uint(_)
| rustc_middle::ty::TyKind::Float(_)
| rustc_middle::ty::TyKind::Str => SimpleTyKind::Primitive,
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => SimpleTyKind::Primitive,

rustc_middle::ty::TyKind::Adt(_, _) => SimpleTyKind::UserDef,
ty::Adt(_, _) => SimpleTyKind::UserDef,

rustc_middle::ty::TyKind::Array(_, _) | rustc_middle::ty::TyKind::Slice(_) => SimpleTyKind::Sequence,
ty::Array(_, _) | ty::Slice(_) => SimpleTyKind::Sequence,

rustc_middle::ty::TyKind::Ref(_, _, _) => SimpleTyKind::Reference,
ty::Ref(_, _, _) => SimpleTyKind::Reference,

rustc_middle::ty::TyKind::Foreign(_) => SimpleTyKind::Foreign,
rustc_middle::ty::TyKind::RawPtr(_) => SimpleTyKind::RawPtr,
ty::Foreign(_) => SimpleTyKind::Foreign,
ty::RawPtr(_, _) => SimpleTyKind::RawPtr,

rustc_middle::ty::TyKind::FnDef(_, _) | rustc_middle::ty::TyKind::FnPtr(_) => SimpleTyKind::Fn,
rustc_middle::ty::TyKind::Closure(_, _) => SimpleTyKind::Closure,
ty::FnDef(_, _) | ty::FnPtr(_) => SimpleTyKind::Fn,
ty::Closure(_, _) => SimpleTyKind::Closure,

rustc_middle::ty::TyKind::Alias(rustc_middle::ty::AliasKind::Opaque, _)
| rustc_middle::ty::TyKind::Dynamic(_, _, _) => SimpleTyKind::TraitObj,
ty::Alias(ty::AliasKind::Opaque, _) | ty::Dynamic(_, _, _) => SimpleTyKind::TraitObj,

rustc_middle::ty::TyKind::Param(_) => SimpleTyKind::Generic,
rustc_middle::ty::TyKind::Bound(_, _) | rustc_middle::ty::TyKind::Alias(_, _) => SimpleTyKind::Idk,
ty::Param(_) => SimpleTyKind::Generic,
ty::Bound(_, _) | ty::Alias(_, _) => SimpleTyKind::Idk,

rustc_middle::ty::TyKind::CoroutineClosure(_, _)
| rustc_middle::ty::TyKind::Coroutine(_, _)
| rustc_middle::ty::TyKind::CoroutineWitness(_, _)
| rustc_middle::ty::TyKind::Placeholder(_)
| rustc_middle::ty::TyKind::Infer(_)
| rustc_middle::ty::TyKind::Error(_) => unreachable!("{ty:#?}"),
ty::CoroutineClosure(_, _)
| ty::Coroutine(_, _)
| ty::CoroutineWitness(_, _)
| ty::Placeholder(_)
| ty::Infer(_)
| ty::Error(_)
| ty::Pat(_, _) => unreachable!("{ty:#?}"),
}
}
}
Expand Down
56 changes: 32 additions & 24 deletions clippy_lints/src/borrow_pats/info/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,20 @@ impl<'a, 'tcx> MetaAnalysis<'a, 'tcx> {
| mir::TerminatorKind::Assert { target, .. }
| mir::TerminatorKind::Call { target: Some(target), .. }
| mir::TerminatorKind::Drop { target, .. }
| mir::TerminatorKind::InlineAsm { destination: Some(target), .. }
| mir::TerminatorKind::Goto { target } => {
self.preds[*target].push(bb);
CfgInfo::Linear(*target)
},
mir::TerminatorKind::InlineAsm { targets, .. } => {
let mut branches = SmallVec::new();
branches.extend(targets.iter().copied());

for target in &branches {
self.preds[*target].push(bb);
}

CfgInfo::Condition { branches }
},
mir::TerminatorKind::SwitchInt { targets, .. } => {
let mut branches = SmallVec::new();
branches.extend_from_slice(targets.all_targets());
Expand All @@ -114,8 +123,7 @@ impl<'a, 'tcx> MetaAnalysis<'a, 'tcx> {
| mir::TerminatorKind::UnwindTerminate(_)
| mir::TerminatorKind::Unreachable
| mir::TerminatorKind::CoroutineDrop
| mir::TerminatorKind::Call { .. }
| mir::TerminatorKind::InlineAsm { .. } => {
| mir::TerminatorKind::Call { .. } => {
CfgInfo::None
},
mir::TerminatorKind::Return => {
Expand All @@ -129,31 +137,31 @@ impl<'a, 'tcx> MetaAnalysis<'a, 'tcx> {
}

fn visit_terminator_for_terms(&mut self, term: &Terminator<'tcx>, bb: BasicBlock) {
if let
mir::TerminatorKind::Call {
func,
args,
destination,
..
} = &term.kind {
assert!(destination.projection.is_empty());
let dest = destination.local;
self.terms.insert(
bb,
calc_call_local_relations(self.tcx.0, self.body, func, dest, args, &mut self.stats),
);
}
if let mir::TerminatorKind::Call {
func,
args,
destination,
..
} = &term.kind
{
assert!(destination.projection.is_empty());
let dest = destination.local;
self.terms.insert(
bb,
calc_call_local_relations(self.tcx.0, self.body, func, dest, args, &mut self.stats),
);
}
}

fn visit_terminator_for_locals(&mut self, term: &Terminator<'tcx>, _bb: BasicBlock) {
if let mir::TerminatorKind::Call { destination, .. } = &term.kind {
// TODO: Should mut arguments be handled?
assert!(destination.projection.is_empty());
let local = destination.local;
self.locals
.get_mut(local)
.unwrap()
.add_assign(*destination, DataInfo::Computed);
// TODO: Should mut arguments be handled?
assert!(destination.projection.is_empty());
let local = destination.local;
self.locals
.get_mut(local)
.unwrap()
.add_assign(*destination, DataInfo::Computed);
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions clippy_lints/src/borrow_pats/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#![expect(unused)]
#![allow(clippy::module_name_repetitions, clippy::default_trait_access, clippy::wildcard_imports ,clippy::enum_variant_names)]
#![allow(
clippy::module_name_repetitions,
clippy::default_trait_access,
clippy::wildcard_imports,
clippy::enum_variant_names
)]
//! # TODOs
//! - [ ] Update meta analysis
//! - [ ] Handle loops by partially retraverse them
Expand Down Expand Up @@ -88,11 +93,11 @@ declare_clippy_lint! {
/// ### Why is this bad?
///
/// ### Example
/// ```no_run
/// ```ignore
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```no_run
/// ```ignore
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "1.78.0"]
Expand Down
18 changes: 10 additions & 8 deletions clippy_lints/src/borrow_pats/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,22 @@ pub enum OwnedPat {
PartArgBorrowMut,
PartArgBorrowMutExtended,
/// Two temp borrows might alias each other, for example like this:
/// ```
/// ```ignore
/// take_2(&self.field, &self.field);
/// ```
/// This also includes fields and sub fields
/// ```
/// ```ignore
/// take_2(&self.field, &self.field.sub_field);
/// ```
AliasedBorrow,
/// A function takes mutliple `&mut` references to different parts of the object
/// ```
/// ```ignore
/// take_2(&mut self.field_a, &mut self.field_b);
/// ```
/// Mutable borrows can't be aliased.
MultipleMutBorrowsInArgs,
/// A function takes both a mutable and an immutable loan as the function input.
/// ```
/// ```ignore
/// take_2(&self.field_a, &mut self.field_b);
/// ```
/// The places can not be aliased.
Expand All @@ -160,7 +160,7 @@ pub enum OwnedPat {
/// This value is involved in a two phased borrow. Meaning that an argument is calculated
/// using the value itself. Example:
///
/// ```
/// ```ignore
/// fn two_phase_borrow_1(mut vec: Vec<usize>) {
/// vec.push(vec.len());
/// }
Expand Down Expand Up @@ -192,7 +192,7 @@ pub enum OwnedPat {
TwoPhasedBorrow,
/// A value is first mutably initilized and then moved into an unmut value.
///
/// ```
/// ```ignore
/// fn mut_and_shadow_immut() {
/// let mut x = "Hello World".to_string();
/// x.push('x');
Expand Down Expand Up @@ -220,7 +220,7 @@ pub enum OwnedPat {
PartOwningAnonDrop,
/// This value is being dropped (by rustc) early to be replaced.
///
/// ```
/// ```ignore
/// let data = String::new();
///
/// // Rustc will first drop the old value of `data`
Expand Down Expand Up @@ -270,7 +270,7 @@ impl<'a, 'tcx> Visitor<'tcx> for OwnedAnalysis<'a, 'tcx> {
}

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

Expand Down Expand Up @@ -438,6 +438,7 @@ impl<'a, 'tcx> OwnedAnalysis<'a, 'tcx> {
}
},
mir::AggregateKind::Coroutine(_, _) | mir::AggregateKind::CoroutineClosure(_, _) => unreachable!(),
mir::AggregateKind::RawPtr(_, _) => {},
}
}
}
Expand Down Expand Up @@ -608,6 +609,7 @@ impl<'a, 'tcx> OwnedAnalysis<'a, 'tcx> {
self.pats.insert(OwnedPat::PartMovedToClosure);
}
},
mir::AggregateKind::RawPtr(_, _) => {},
mir::AggregateKind::Coroutine(_, _) | mir::AggregateKind::CoroutineClosure(_, _) => unreachable!(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/borrow_pats/owned/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct StateInfo<'tcx> {
///
/// **Note 1**: Named borrows can be created in two ways (Because of course
/// they can...)
/// ```
/// ```ignore
/// // From: `mut_named_ref_non_kill`
/// // let mut x = 1;
/// // let mut p: &u32 = &x;
Expand All @@ -38,7 +38,7 @@ pub struct StateInfo<'tcx> {
/// **Note 2**: Correction there are three ways to created named borrows...
/// Not sure why but let's take `mut_named_ref_non_kill` as and example for `y`
///
/// ```
/// ```ignore
/// // y => _2
/// // named => _3
/// _8 = &_2
Expand Down Expand Up @@ -95,7 +95,7 @@ pub struct BorrowInfo<'tcx> {
/// The place that is being borrowed
pub broker: Place<'tcx>,
/// This is the mutability of the original borrow. If we have a double borrow, like this:
/// ```
/// ```ignore
/// let mut data = String::new();
///
/// // Loan 1
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/borrow_pats/rustc_extention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl PlaceMagic for mir::Place<'_> {
}

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

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/borrow_pats/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl CrateStats {
/// owned value and `_2` is the named references, they could have the following
/// shapes:
///
/// ```
/// ```ignore
/// // Direct
/// _2 = &_1
///
Expand Down Expand Up @@ -178,7 +178,7 @@ pub struct OwnedStats {
/// Temp borrows are used for function calls.
///
/// The MIR commonly looks like this:
/// ```
/// ```ignore
/// _3 = &_1
/// _4 = &(*_3)
/// _2 = function(move _4)
Expand Down
9 changes: 4 additions & 5 deletions clippy_lints/src/borrow_pats/util.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#![warn(unused)]
use std::collections::{BTreeMap, BTreeSet};

use clippy_utils::ty::{for_each_param_ty, for_each_ref_region, for_each_region};
use rustc_ast::Mutability;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::mir::{Body, Local, Operand, Place};
use rustc_middle::ty::{FnSig, GenericArgsRef, GenericPredicates, Region, Ty, TyCtxt, TyKind};
use rustc_middle::ty::{self, FnSig, GenericArgsRef, GenericPredicates, Region, Ty, TyCtxt};
use rustc_span::source_map::Spanned;

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

0 comments on commit 27749e5

Please sign in to comment.