-
Notifications
You must be signed in to change notification settings - Fork 988
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
Detector for unused imports #1592
Conversation
Thanks for tackling this! We definitely want to have this, but will likely need to address the issues you outline.
You may be able to iterate over the compilation unit and contract scope to get this info. Wrt |
@0xalpharush Thanks a lot for the feedback! I will try to make use of these An additional problem that I just realised is within the function |
I have just checked both options (using compilation unit and scope) and indeed, I can iterate over user defined types (aliases), but their references aren't updated correctly.
I would prefer the first option, because the code will be easier to read and maintain in the future + we will already have tests for the |
""" | ||
for ref in item.references: | ||
self._add_import( | ||
self.needed_imports, ref.filename.absolute, item.source_mapping.filename.absolute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may need to do something like Path(...).absolute() and then use filename_loop in _import_to_absolute_path
@bart1e I'm not exactly sure what's incorrect but you're welcome to make changes that fix it and help make your detector more accurate slither/slither/core/slither_core.py Line 236 in 4c976d5
|
@0xalpharush, I will try to make the changes as you suggested. Can I do them in this PR, or create a separate one? And, perhaps, I have expressed myself wrong previously - by "but their references aren't updated correctly" I meant that slither/slither/solc_parsing/solidity_types/type_parsing.py Lines 359 to 365 in 4c976d5
|
@bart1e A separate PR would make it a little easier to review/ test in isolation |
@0xalpharush OK, I will try to submit a separate PR tomorrow. |
2a4fd8b
to
2268bb0
Compare
Would also be very helpful to detect unused custom errors and modifiers. |
db220ff
to
5a502e4
Compare
continue | ||
output += ( | ||
"Unused imports found in " | ||
+ PurePath(os.path.relpath(k, os.curdir)).as_posix() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not 100% sure, but I think we can get rid of the local/CI file path discrepancies by using PurePath(k).as_posix()
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made the change you suggested, however, for some reason, tests weren't run on the code, so I am not sure if it passes the tests that were failing.
@0xalpharush Before this will be merged, I have to know how the detector should deal with the following things:
// file C.sol uses B.sol and A.sol and contains the following imports:
import "./A.sol";
import "./B.sol";
// file B.sol doesn't use anything from A.sol, but contains the import:
import "./A.sol"; Currently, the detector will say that |
I think it'd make sense to ignore files that only contain imports but if a file imports this "container" we'd still want to warn for unused ones. I think leaving it as is for right now is fine. If it becomes a nuisance for users down the line, we can improve the heuristic. |
I have added a code that will ignore "import containers" while printing the output. In the future, I may also handle cyclic imports (right now, if cyclic imports are detected, the detector simply omits the files that contain them) - I think that would be too much for one PR as the code is already complicated enough. |
- Use .values instead of .items() with unused key - Improve py types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an amazing work, thanks @bart1e for the effort.
I made minor modifications (mostly python related). My main concern is around the class variable (see the comment)
We determined that we don't want to identify "redundant" imports i.e. where a dependency can be satisfied transitively and instead only recommend removing absolutely unused imports |
…o detector_for_unused_imports
I've removed the code responsible for detecting "redundant" imports. Now, only imports whose own import graph doesn't contain any needed import will be listed. So, for example, if we have So, the detector will now print something a lot less frequently than with the previous implementation. |
Just wanted to bump this and see if there are plans to get this merged anytime soon :) |
I will try to get this updated and ready for the next release (probably a couple weeks). In addition to some testing, I want to change the way the file names are displayed to be relative to the project and not include the user's home as well as change the format a bit.
To:
|
Detector for unused imports implemented. Fixes #1531.
Files with with cyclic imports and files with
import {...} from
directives are not supported.Apart from telling the user which imports to remove, it may tell to add more specific imports. For instance if we have:
, then it will tell that
import "./B.sol"
should be removed inC.sol
andimport "./A.sol"
should be added.For each file, it searches for all items used there and updates the list of needed imports.
Unfortunately, it isn't currently possible to iterate over user defined types in Slither (aliases like
type something is uint
), so I had to handle them "manually".Additionally, some items don't have their
references
set up correctly. Some examples of that include:Some issues regarding the implementation:
import
directives with related files. The detector will tell that they contain unused imports, which is correct, but creating such files is intentional. You may see it by running the detector on @openzeppelin/contracts/interfaces.You are welcome to test the detector, and if you have any suggestion on how to improve it, or any question regarding the implementation, please let me know.