Skip to content

Commit

Permalink
Better anon ref tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Apr 18, 2024
1 parent b169331 commit e2414a7
Show file tree
Hide file tree
Showing 6 changed files with 1,737 additions and 4 deletions.
5 changes: 4 additions & 1 deletion clippy_lints/src/borrow_pats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ impl<'tcx> LateLintPass<'tcx> for BorrowPats {
// eprintln!("{body:#?}");
print_debug_info(cx, body, def);
}

if lint_level != Level::Allow {
let mut info = AnalysisInfo::new(cx, def);
if lint_level == Level::Forbid {
println!("{info:#?}");
}

info.return_pats = ret::ReturnAnalysis::run(&info);

Expand Down
18 changes: 16 additions & 2 deletions clippy_lints/src/borrow_pats/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{visit_body_in_order, AnalysisInfo, LocalKind, PatternEnum, PatternSt

use hir::Mutability;
use mid::mir::visit::{MutatingUseContext, Visitor};
use mid::mir::{Operand, VarDebugInfo, VarDebugInfoContents, START_BLOCK};
use mid::mir::{Operand, StatementKind, VarDebugInfo, VarDebugInfoContents, START_BLOCK};
use mid::ty::TypeVisitableExt;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_index::IndexVec;
Expand Down Expand Up @@ -236,6 +236,7 @@ impl<'a, 'tcx> Visitor<'tcx> for OwnedAnalysis<'a, 'tcx> {
self.use_count += 1;
}
}

}

impl<'a, 'tcx> OwnedAnalysis<'a, 'tcx> {
Expand All @@ -259,7 +260,7 @@ impl<'a, 'tcx> OwnedAnalysis<'a, 'tcx> {
assert!(!target.has_projections());
self.states[bb].anons.insert(target.local);
} else {
todo!("{target:#?}\n{rval:#?}\n{self:#?}");
todo!("{target:#?} = {rval:#?} (at {bb:#?})\n{self:#?}");
}
} else {
// Copies are uninteresting to me
Expand Down Expand Up @@ -330,6 +331,19 @@ impl<'a, 'tcx> OwnedAnalysis<'a, 'tcx> {
LocalKind::Unused => unreachable!(),
}
}

if let mir::Rvalue::Ref(_reg, _kind, src) = &rval
&& let Some(muta) = self.states[bb].temp_bros.get(&src.local).copied()
{
match src.projection.as_slice() {
[mir::PlaceElem::Deref] => {
// &(*_1) = Copy
assert!(!target.has_projections());
self.states[bb].temp_bros.insert(target.local, muta);
},
_ => todo!("Handle ref of anon ref {target:#?} = {rval:#?} (at {bb:#?})\n{self:#?}"),
}
}
}

fn visit_terminator_for_args(&mut self, term: &mir::Terminator<'tcx>, bb: BasicBlock) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/thesis/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn pat_maybe_return_owned_arg_1_test(a: u32) -> u32 {
19
}

#[warn(clippy::borrow_pats)]
#[forbid(clippy::borrow_pats)]
/// FIXME: The argument return is not yet detected both in `a` and `Return`
fn pat_maybe_return_owned_arg_2(a: String) -> String {
let ret;
Expand Down
Loading

0 comments on commit e2414a7

Please sign in to comment.