Skip to content

Commit

Permalink
Fix linters issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkaMaul committed Apr 17, 2024
1 parent 9d2f280 commit 9e223dc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
12 changes: 5 additions & 7 deletions slither/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3

import argparse
import cProfile
import glob
Expand Down Expand Up @@ -53,6 +52,8 @@
)
from slither.exceptions import SlitherException

# pylint: disable=too-many-lines

logging.basicConfig()
logger = logging.getLogger("Slither")

Expand Down Expand Up @@ -300,8 +301,8 @@ def parse_option(element: str, option: str) -> FilteringRule:
if match.group("function")
else None,
)
except re.error:
raise ValueError(f"Unable to parse option {element}, invalid regex.")
except re.error as exc:
raise ValueError(f"Unable to parse option {element}, invalid regex.") from exc

raise ValueError(f"Unable to parse option {element}")

Expand Down Expand Up @@ -375,7 +376,7 @@ def parse_args(
the target for the detectors and/or printers can be restricted using these options.
The filtering allow to to select directories, files, contract and functions. Each part
is compiled as a regex (so A*\.sol) will match every file that starts with A and ends with .sol.
is compiled as a regex (so A*.sol) will match every file that starts with A and ends with .sol.
Examples :
- `sub1/A.sol:A.constructor` will analyze only the function named constructor in the contract A
Expand Down Expand Up @@ -737,9 +738,6 @@ def parse_args(

args = parser.parse_args()
read_config_file(args)

# args.filter_paths = parse_filter_paths(args, True)
# args.include_paths = parse_filter_paths(args, False)
args.filters = parse_filter_paths(args)

# Verify our json-type output is valid
Expand Down
1 change: 0 additions & 1 deletion slither/core/compilation_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from slither.core.declarations.function_top_level import FunctionTopLevel
from slither.core.declarations.structure_top_level import StructureTopLevel
from slither.core.declarations.using_for_top_level import UsingForTopLevel
from slither.core.filtering import FilteringRule
from slither.core.scope.scope import FileScope
from slither.core.solidity_types.type_alias import TypeAliasTopLevel
from slither.core.variables.state_variable import StateVariable
Expand Down
14 changes: 8 additions & 6 deletions slither/core/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def match_contract(self, contract: "Contract") -> bool:

# If we have no constraint, we just follow the default rule
if self.path is None and self.contract is None:
return True if self.type == FilteringAction.ALLOW else False
return self.type == FilteringAction.ALLOW

path_match = None
if self.path is not None:
Expand All @@ -48,17 +48,19 @@ def match_contract(self, contract: "Contract") -> bool:

if path_match is None:
return contract_match
elif contract_match is None:

if contract_match is None:
return path_match
elif contract_match and path_match:

if contract_match and path_match:
return True
else:
return False

return False

def match_function(self, function: "FunctionContract") -> bool:
"""Check if this filter apply to this element."""
# If we have no constraint, follow default rule
if self.function is None:
return True if self.type == FilteringAction.ALLOW else False
return self.type == FilteringAction.ALLOW

return bool(re.search(self.function, function.name))
8 changes: 4 additions & 4 deletions slither/core/slither_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ def filter_contract(self, contract: Contract) -> bool:

if action == FilteringAction.ALLOW:
return False
else:
return True

return True

def filter_function(self, function: "FunctionContract") -> bool:
"""Checks within the filter if this function should be excluded."""
Expand All @@ -368,8 +368,8 @@ def filter_function(self, function: "FunctionContract") -> bool:

if action == FilteringAction.ALLOW:
return False
else:
return True

return True

def parse_ignore_comments(self, file: str) -> None:
# The first time we check a file, find all start/end ignore comments and memoize them.
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/filtering/test_data/test_filtering/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Foundry
# Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Expand Down
12 changes: 7 additions & 5 deletions tests/e2e/filtering/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def run_detector_for_filtering(
):

# First, reset any results
# pylint: disable=protected-access
sl._currently_seen_resuts = set()

# Set up default action
Expand Down Expand Up @@ -192,14 +193,14 @@ def get_default_namespace(
filter_paths: Union[str, None] = None,
include: Union[str, None] = None,
remove: Union[str, None] = None,
filter: Union[str, None] = None,
filter_arg: Union[str, None] = None,
) -> Namespace:
return Namespace(
include_paths=include_paths,
filter_paths=filter_paths,
include=include,
remove=remove,
filter=filter,
filter=filter_arg,
)


Expand Down Expand Up @@ -256,7 +257,7 @@ def test_filtering_cl_full():


def test_filtering_cl_filter():
parsed_args = parse_filter_paths(get_default_namespace(filter="sub1/,-sub1/sub12/"))
parsed_args = parse_filter_paths(get_default_namespace(filter_arg="sub1/,-sub1/sub12/"))
assert parsed_args == [
FilteringRule(
type=FilteringAction.ALLOW,
Expand All @@ -271,10 +272,10 @@ def test_filtering_cl_filter():

def test_invalid_regex():
with pytest.raises(ValueError):
parse_filter_paths(get_default_namespace(filter="sub1(/"))
parse_filter_paths(get_default_namespace(filter_arg="sub1(/"))

with pytest.raises(ValueError):
parse_filter_paths(get_default_namespace(filter=":not-matching:"))
parse_filter_paths(get_default_namespace(filter_arg=":not-matching:"))


@pytest.mark.skipif(
Expand All @@ -285,6 +286,7 @@ def run_printer(
sl: Slither, filtering_rules: List[FilteringRule], default_action: FilteringAction
):
sl.filters = filtering_rules
# pylint: disable=protected-access
sl._contracts = [] # Reset the list of contracts so it gets recomputed
sl.default_action = default_action
return DummyPrinter.analyze_dummy_output(sl.run_printers().pop())
Expand Down

0 comments on commit 9e223dc

Please sign in to comment.