Skip to content

Commit

Permalink
Thesis: Humble beginings
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Mar 7, 2024
1 parent e485a02 commit 21e3a40
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5066,6 +5066,7 @@ Released 2018-09-13
[`borrow_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_as_ptr
[`borrow_deref_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_deref_ref
[`borrow_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const
[`borrow_pats`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_pats
[`borrowed_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box
[`box_collection`]: https://rust-lang.github.io/rust-clippy/master/index.html#box_collection
[`box_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#box_default
Expand Down
56 changes: 56 additions & 0 deletions clippy_lints/src/borrow_pats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;

use rustc_middle::mir as mir;

declare_clippy_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Example
/// ```no_run
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```no_run
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "1.78.0"]
pub BORROW_PATS,
nursery,
"default lint description"
}

declare_lint_pass!(BorrowPats => [BORROW_PATS]);

impl<'tcx> LateLintPass<'tcx> for BorrowPats {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx hir::Body<'tcx>) {
// if in_external_macro(cx.tcx.sess, body.value.span) && is_from_proc_macro(cx, &(&kind, body, body.value.hir_id, body.value.span)) {
// return;
// }

let def = cx.tcx.hir().body_owner_def_id(body.id());


// let mir = cx.tcx.mir_built(def).borrow();
// let mir = cx.tcx.mir_promoted(def);
let mir = cx.tcx.optimized_mir(def);
print_body(mir);
}
}

fn print_body(body: &mir::Body<'_>) {
for (idx, data) in body.basic_blocks.iter_enumerated() {
println!("bb{}:", idx.index());
for stmt in &data.statements {
println!(" {stmt:#?}");
}
println!(" {:#?}", data.terminator().kind);

println!();
}

println!("{body:#?}");
}
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::booleans::NONMINIMAL_BOOL_INFO,
crate::booleans::OVERLY_COMPLEX_BOOL_EXPR_INFO,
crate::borrow_deref_ref::BORROW_DEREF_REF_INFO,
crate::borrow_pats::BORROW_PATS_INFO,
crate::box_default::BOX_DEFAULT_INFO,
crate::cargo::CARGO_COMMON_METADATA_INFO,
crate::cargo::LINT_GROUPS_PRIORITY_INFO,
Expand Down
6 changes: 6 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ mod bool_assert_comparison;
mod bool_to_int_with_if;
mod booleans;
mod borrow_deref_ref;
mod borrow_pats;
mod box_default;
mod cargo;
mod casts;
Expand Down Expand Up @@ -632,6 +633,11 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
});
}

store.register_late_pass(|_| Box::new(borrow_pats::BorrowPats));
if !std::env::var("ENABLE_ALL_LINTS").eq(&Ok("1".to_string())) {
return;
}

store.register_late_pass(move |_| {
Box::new(operators::arithmetic_side_effects::ArithmeticSideEffects::new(
arithmetic_side_effects_allowed
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/borrow_pats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![warn(clippy::borrow_pats)]

fn main() {
// test code goes here
}
20 changes: 20 additions & 0 deletions tests/ui/thesis/simple_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![allow(unused)]


fn main() {

// let owned_1 = String::new();
//
// {
// let owned_2 = String::new();
// let owned_3 = String::new();
//
// drop(owned_2);
// }

let owned_4b = {
let owned_4a = String::new();

owned_4a
};
}

0 comments on commit 21e3a40

Please sign in to comment.