Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
733 changes: 447 additions & 286 deletions Cargo.lock

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exclude = ["test_app"]

[package]
name = "cargo_pup"
version = "0.1.6"
version = "0.1.7"
edition = "2024"
description = "A Rust architectural linting tool that integrates with rustc to enforce architectural patterns and boundaries"
license = "Apache-2.0"
Expand All @@ -20,23 +20,23 @@ categories = ["development-tools"]
readme = "README.md"

[workspace.dependencies]
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
anyhow = "1.0.95"
regex = "1.11.1"
tempfile = "3.14.0"
ron = "0.8.1"
cargo_metadata = "0.18"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
anyhow = "1.0.102"
regex = "1.12.3"
tempfile = "3.27.0"
ron = "0.12.1"
cargo_metadata = "0.23"

[dependencies]
ansi_term = "0.12.1"
anyhow = { workspace = true }
tempfile = { workspace = true }
ron = { workspace = true }
cargo_metadata = { workspace = true }
cargo_pup_common = { path = "cargo_pup_common", version = "=0.1.6" }
cargo_pup_lint_impl = { path = "cargo_pup_lint_impl", version = "=0.1.6" }
cargo_pup_lint_config = { path = "cargo_pup_lint_config", version = "=0.1.6" }
cargo_pup_common = { path = "cargo_pup_common", version = "=0.1.7" }
cargo_pup_lint_impl = { path = "cargo_pup_lint_impl", version = "=0.1.7" }
cargo_pup_lint_config = { path = "cargo_pup_lint_config", version = "=0.1.7" }

#
# These bits are just to keep rust rover happy.
Expand All @@ -51,13 +51,13 @@ cargo_pup_lint_config = { path = "cargo_pup_lint_config", version = "=0.1.6" }
# rustc_lint = {path = "/Users/scott.gerring/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/rustc-src/rust/compiler/rustc_lint", optional = true}
# rustc_trait_selection = {path = "/Users/scott.gerring/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/rustc-src/rust/compiler/rustc_trait_selection", optional = true}
# rustc_errors = {path = "/Users/scott.gerring/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/rustc-src/rust/compiler/rustc_errors", optional = true}
toml = "0.8.19"
which = "7.0.1"
rustup-toolchain = "0.1.8"
toml = "1.1.1"
which = "8.0.2"
rustup-toolchain = "0.1.10"

[dev-dependencies]
rustc_version = "0.4"
ui_test = "0.29.2"
ui_test = "0.30.4"
ron = { workspace = true }

[[test]]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ First, add the following to your `Cargo.toml`:

```toml
[dev-dependencies]
cargo_pup_lint_config = "0.1.6"
cargo_pup_lint_config = "0.1.7"
```

## Examples
Expand Down
2 changes: 1 addition & 1 deletion cargo_pup_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo_pup_common"
version = "0.1.6"
version = "0.1.7"
edition = "2024"
description = "Common utilities and shared components for cargo-pup architectural linting tool"
license = "Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions cargo_pup_lint_config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo_pup_lint_config"
version = "0.1.6"
version = "0.1.7"
edition = "2024"
description = "Configuration and rule builder utilities for cargo-pup architectural linting"
license = "Apache-2.0"
Expand All @@ -13,4 +13,4 @@ serde.workspace = true
ron.workspace = true
tempfile.workspace = true
anyhow.workspace = true
cargo_pup_common = { path = "../cargo_pup_common", version = "=0.1.6" }
cargo_pup_common = { path = "../cargo_pup_common", version = "=0.1.7" }
2 changes: 1 addition & 1 deletion cargo_pup_lint_config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Add this to your `Cargo.toml`:

```toml
[dev-dependencies]
cargo_pup_lint_config = "0.1.6"
cargo_pup_lint_config = "0.1.7"
```

## Example
Expand Down
9 changes: 6 additions & 3 deletions cargo_pup_lint_config/src/lint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use ron::de::from_reader;
use ron::ser::{PrettyConfig, to_writer_pretty};
use std::fs::File;
use std::io;
use std::io::{self, Write};
// lint_builder.rs
use crate::function_lint::FunctionLint;
use crate::module_lint::ModuleLint;
Expand Down Expand Up @@ -65,8 +65,11 @@ impl LintBuilder {

// Method to write the LintBuilder to a file
pub fn write_to_file<P: AsRef<std::path::Path>>(&self, path: P) -> io::Result<()> {
let file = File::create(path).map_err(io::Error::other)?;
to_writer_pretty(file, &self, PrettyConfig::default()).map_err(io::Error::other)?;
let mut content = String::new();
to_writer_pretty(&mut content, &self, PrettyConfig::default()).map_err(io::Error::other)?;
let mut file = File::create(path).map_err(io::Error::other)?;
file.write_all(content.as_bytes())
.map_err(io::Error::other)?;
Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions cargo_pup_lint_impl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo_pup_lint_impl"
version = "0.1.6"
version = "0.1.7"
edition = "2024"
description = "Core lint implementations and rustc integration for cargo-pup architectural linting"
license = "Apache-2.0"
Expand All @@ -9,8 +9,8 @@ homepage = "https://github.com/datadog/cargo-pup"
readme = "README.md"

[dependencies]
cargo_pup_lint_config = { path = "../cargo_pup_lint_config", version = "=0.1.6" }
cargo_pup_common = { path = "../cargo_pup_common", version = "=0.1.6" }
cargo_pup_lint_config = { path = "../cargo_pup_lint_config", version = "=0.1.7" }
cargo_pup_common = { path = "../cargo_pup_common", version = "=0.1.7" }
anyhow.workspace = true
regex.workspace = true
serde_json.workspace = true
Expand Down
24 changes: 23 additions & 1 deletion cargo_pup_lint_impl/src/lints/function_lint/no_allocation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/) Copyright 2024 Datadog, Inc.

use rustc_hir::def_id::DefId;
use rustc_middle::mir::{Body, TerminatorKind};
use rustc_middle::mir::{Body, Rvalue, StatementKind, TerminatorKind};
use rustc_middle::ty::TyCtxt;
use rustc_span::Span;
use std::collections::HashMap;
Expand All @@ -20,6 +20,28 @@ pub fn detect_allocation_in_mir<'tcx>(
_fn_def_id: DefId,
cache: &mut HashMap<DefId, bool>,
) -> Option<AllocationViolation> {
// Async functions are desugared into a thin wrapper whose MIR just
// constructs a coroutine via Rvalue::Aggregate(Coroutine(..)). The
// actual user code lives in the coroutine body, so we must follow
// into it to detect allocations.
for bb_data in mir.basic_blocks.iter() {
for stmt in &bb_data.statements {
if let StatementKind::Assign(assign) = &stmt.kind
&& let Rvalue::Aggregate(kind, _) = &assign.1
&& let rustc_middle::mir::AggregateKind::Coroutine(def_id, _) = &**kind
&& def_id.krate == rustc_hir::def_id::LOCAL_CRATE
&& tcx.is_mir_available(*def_id)
{
let coroutine_mir = tcx.optimized_mir(*def_id);
if let Some(violation) =
detect_allocation_in_mir(tcx, coroutine_mir, *def_id, cache)
{
return Some(violation);
}
}
}
}

// Iterate through basic blocks
for (_bb, bb_data) in mir.basic_blocks.iter_enumerated() {
// Check terminator for calls
Expand Down
22 changes: 21 additions & 1 deletion cargo_pup_lint_impl/src/lints/function_lint/no_panic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/) Copyright 2024 Datadog, Inc.

use rustc_hir::def_id::DefId;
use rustc_middle::mir::{AssertKind, Body, TerminatorKind};
use rustc_middle::mir::{AssertKind, Body, Rvalue, StatementKind, TerminatorKind};
use rustc_middle::ty::TyCtxt;
use rustc_span::Span;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -96,6 +96,26 @@ fn analyze_mir<'tcx>(
cache: &mut HashMap<DefId, bool>,
categories: &HashSet<PanicCategory>,
) -> Option<PanicViolation> {
// Async functions are desugared into a thin wrapper whose MIR just
// constructs a coroutine via Rvalue::Aggregate(Coroutine(..)). The
// actual user code lives in the coroutine body, so we must follow
// into it to detect panics.
for bb_data in mir.basic_blocks.iter() {
for stmt in &bb_data.statements {
if let StatementKind::Assign(assign) = &stmt.kind
&& let Rvalue::Aggregate(kind, _) = &assign.1
&& let rustc_middle::mir::AggregateKind::Coroutine(def_id, _) = &**kind
&& def_id.krate == rustc_hir::def_id::LOCAL_CRATE
&& tcx.is_mir_available(*def_id)
{
let coroutine_mir = tcx.optimized_mir(*def_id);
if let Some(violation) = analyze_mir(tcx, coroutine_mir, cache, categories) {
return Some(violation);
}
}
}
}

for (_bb, bb_data) in mir.basic_blocks.iter_enumerated() {
let Some(terminator) = &bb_data.terminator else {
continue;
Expand Down
Loading
Loading