Skip to content

feat(forge lint): unused imports #10662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
59 changes: 29 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ solar-ast = { version = "=0.1.4", default-features = false }
solar-parse = { version = "=0.1.4", default-features = false }
solar-interface = { version = "=0.1.4", default-features = false }
solar-sema = { version = "=0.1.4", default-features = false }
solar-data-structures = { version = "=0.1.4", default-features = false }

## alloy
alloy-consensus = { version = "1.0.11", default-features = false }
Expand Down Expand Up @@ -410,3 +411,10 @@ zip-extract = "=0.2.1"
## foundry
# foundry-compilers = { git = "https://github.com/foundry-rs/compilers.git", rev = "e4a9b04" }
# foundry-fork-db = { git = "https://github.com/foundry-rs/foundry-fork-db", rev = "811a61a" }

# solar
solar-ast = { git = "https://github.com/paradigmxyz/solar.git", branch = "main" }
solar-parse = { git = "https://github.com/paradigmxyz/solar.git", branch = "main" }
solar-interface = { git = "https://github.com/paradigmxyz/solar.git", branch = "main" }
solar-sema = { git = "https://github.com/paradigmxyz/solar.git", branch = "main" }
solar-data-structures = { git = "https://github.com/paradigmxyz/solar.git", branch = "main" }
Comment on lines +416 to +420
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK with pinning to main. Context here: #10662 (review)

1 change: 1 addition & 0 deletions crates/lint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ foundry-config.workspace = true
solar-parse.workspace = true
solar-ast.workspace = true
solar-interface = { workspace = true, features = ["json"] }
solar-data-structures.workspace = true

heck.workspace = true
rayon.workspace = true
Expand Down
68 changes: 65 additions & 3 deletions crates/lint/src/linter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use foundry_compilers::Language;
use foundry_config::lint::Severity;
use solar_ast::{visit::Visit, Expr, ItemFunction, ItemStruct, VariableDefinition};
use solar_ast::{
visit::Visit, Expr, ImportDirective, ItemContract, ItemFunction, ItemStruct, SourceUnit,
UsingDirective, VariableDefinition,
};
use solar_interface::{
data_structures::Never,
diagnostics::{DiagBuilder, DiagId, MultiSpan},
Expand Down Expand Up @@ -47,7 +50,7 @@ impl<'s> LintContext<'s> {
Self { sess, with_description, inline_config: config }
}

// Helper method to emit diagnostics easily from passes
/// Helper method to emit diagnostics easily from passes
pub fn emit<L: Lint>(&self, lint: &'static L, span: Span) {
if self.inline_config.is_disabled(span, lint.id()) {
return;
Expand Down Expand Up @@ -78,8 +81,25 @@ pub trait EarlyLintPass<'ast>: Send + Sync {
_var: &'ast VariableDefinition<'ast>,
) {
}

fn check_import_directive(
&mut self,
_ctx: &LintContext<'_>,
_import: &'ast ImportDirective<'ast>,
) {
}
fn check_using_directive(
&mut self,
_ctx: &LintContext<'_>,
_using: &'ast UsingDirective<'ast>,
) {
}
fn check_item_contract(&mut self, _ctx: &LintContext<'_>, _contract: &'ast ItemContract<'ast>) {
}
// TODO: Add methods for each required AST node type

/// Should be called after the source unit has been visited. Enables lints that require
/// knowledge of the entire AST to perform their analysis.
fn check_full_source_unit(&mut self, _ctx: &LintContext<'_>, _ast: &'ast SourceUnit<'ast>) {}
}

/// Visitor struct for `EarlyLintPass`es
Expand All @@ -88,6 +108,18 @@ pub struct EarlyLintVisitor<'a, 's, 'ast> {
pub passes: &'a mut [Box<dyn EarlyLintPass<'ast> + 's>],
}

/// Extends the [`Visit`] trait functionality with a hook that can run after the initial traversal.
impl<'s, 'ast> EarlyLintVisitor<'_, 's, 'ast>
where
's: 'ast,
{
pub fn post_source_unit(&mut self, ast: &'ast SourceUnit<'ast>) {
for pass in self.passes.iter_mut() {
pass.check_full_source_unit(self.ctx, ast);
}
}
}

impl<'s, 'ast> Visit<'ast> for EarlyLintVisitor<'_, 's, 'ast>
where
's: 'ast,
Expand Down Expand Up @@ -131,6 +163,36 @@ where
self.walk_item_function(func)
}

fn visit_import_directive(
&mut self,
import: &'ast ImportDirective<'ast>,
) -> ControlFlow<Self::BreakValue> {
for pass in self.passes.iter_mut() {
pass.check_import_directive(self.ctx, import);
}
self.walk_import_directive(import)
}

fn visit_using_directive(
&mut self,
using: &'ast UsingDirective<'ast>,
) -> ControlFlow<Self::BreakValue> {
for pass in self.passes.iter_mut() {
pass.check_using_directive(self.ctx, using);
}
self.walk_using_directive(using)
}

fn visit_item_contract(
&mut self,
contract: &'ast ItemContract<'ast>,
) -> ControlFlow<Self::BreakValue> {
for pass in self.passes.iter_mut() {
pass.check_item_contract(self.ctx, contract);
}
self.walk_item_contract(contract)
}

// TODO: Add methods for each required AST node type, mirroring `solar_ast::visit::Visit` method
// sigs + adding `LintContext`
}
Loading
Loading