Skip to content

Commit

Permalink
Merged dev
Browse files Browse the repository at this point in the history
  • Loading branch information
alexroan committed Mar 6, 2025
2 parents 283b2c3 + 3dd0769 commit c9cbcd9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
29 changes: 13 additions & 16 deletions aderyn_core/src/detect/low/useless_error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
ast::{Expression, NodeID},
ast::NodeID,
capture,
context::workspace_context::WorkspaceContext,
detect::detector::{IssueDetector, IssueDetectorNamePool, IssueSeverity},
Expand All @@ -19,27 +19,24 @@ pub struct UselessErrorDetector {
impl IssueDetector for UselessErrorDetector {
fn detect(&mut self, context: &WorkspaceContext) -> Result<bool, Box<dyn Error>> {
let error_definitions = context.error_definitions().into_iter().collect::<Vec<_>>();
let mut used_errors = HashSet::new();
let mut referenced_ids = HashSet::new();

// Collect all used errors from revert statements
for revert_stmt in context.revert_statements() {
// extract the ids directly from the expression of the function call
if let Expression::Identifier(identifier) = &*revert_stmt.error_call.expression {
if let Some(reference_id) = identifier.referenced_declaration {
used_errors.insert(reference_id);
}
} else if let Expression::MemberAccess(member_access) =
&*revert_stmt.error_call.expression
{
if let Some(reference_id) = member_access.referenced_declaration {
used_errors.insert(reference_id);
}
//Get all MemberAccess and Identifier nodes where the referenced_declaration is an ID of an
// error definition
for identifier in context.identifiers() {
if let Some(reference_id) = identifier.referenced_declaration {
referenced_ids.insert(reference_id);
}
}
for member_access in context.member_accesses() {
if let Some(reference_id) = member_access.referenced_declaration {
referenced_ids.insert(reference_id);
}
}

// Identify unused errors by comparing defined and used error IDs
for error_def in error_definitions {
if !used_errors.contains(&error_def.id) {
if !referenced_ids.contains(&error_def.id) {
// Capture unused error instances
capture!(self, context, error_def);
}
Expand Down
16 changes: 8 additions & 8 deletions reports/report.json

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

10 changes: 5 additions & 5 deletions reports/report.md

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

4 changes: 2 additions & 2 deletions reports/report.sarif

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

7 changes: 6 additions & 1 deletion tests/contract-playground/src/UnusedError.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
pragma solidity 0.8.27;

library ErrorLibrary {
error UnusedLibraryError();
error UsedInARequire();
error LibraryError();
}

Expand All @@ -23,4 +24,8 @@ contract UnusedError {
function goodLibraryError() external pure {
revert ErrorLibrary.LibraryError();
}

function goodRequire() external pure {
require(false, ErrorLibrary.UsedInARequire());
}
}

0 comments on commit c9cbcd9

Please sign in to comment.