Skip to content
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

fix: do not flag imports from import container as unused #2471

Merged
merged 2 commits into from
Jun 5, 2024

Conversation

0xalpharush
Copy link
Contributor

@0xalpharush 0xalpharush commented Jun 5, 2024

closes #2466

Tested on https://github.com/0xalpharush/2466

Summary by CodeRabbit

  • New Features
    • Improved detection of unused imports by skipping dependency files and considering files with only import directives and pragmas.

Copy link

coderabbitai bot commented Jun 5, 2024

Walkthrough

Walkthrough

The recent changes in unused_import.py introduce a new method _is_import_container that checks if a file contains only import directives and pragmas. Additionally, the _detect method logic is updated to skip dependency files and consider import containers when identifying unused imports. These enhancements aim to improve the accuracy of detecting unused imports, particularly in scenarios involving interface inheritance.

Changes

Files/Paths Change Summary
slither/detectors/statements/unused_import.py Added _is_import_container method to check if a file contains only import directives and pragmas. Updated _detect method to skip dependencies and consider import containers when detecting unused imports.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant UnusedImport
    participant FileScope

    User->>UnusedImport: Run unused import detector
    UnusedImport->>FileScope: Analyze file for imports
    FileScope-->>UnusedImport: Return file scope details
    UnusedImport->>UnusedImport: Check if file is an import container (_is_import_container)
    alt File is an import container
        UnusedImport->>UnusedImport: Skip unused import detection
    else File is not an import container
        UnusedImport->>UnusedImport: Continue with unused import detection
    end
    UnusedImport-->>User: Return unused import detection results
Loading

Assessment against linked issues

Objective Addressed Explanation
Correct the detector to avoid mistaking interface inheritance as unused imports (#2466)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@0xalpharush 0xalpharush marked this pull request as ready for review June 5, 2024 21:00
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 57842b0 and 91d5f89.

Files selected for processing (1)
  • slither/detectors/statements/unused_import.py (2 hunks)
Additional comments not posted (1)
slither/detectors/statements/unused_import.py (1)

72-98: The updates to the _detect method to handle import containers and skip dependencies are aligned with the PR objectives.

Ensure that these changes integrate smoothly with the rest of the system, particularly in how they interact with other components of the slither tool.

Comment on lines +34 to +64
@staticmethod
def _is_import_container(scope: FileScope) -> bool: # pylint: disable=too-many-branches
"""
Returns True if a given file (provided as a `FileScope` object) contains only `import` directives (and pragmas).
Such a file doesn't need the imports it contains, but its purpose is to aggregate certain correlated imports.
"""
for c in scope.contracts.values():
if c.file_scope == scope:
return False
for err in scope.custom_errors:
if err.file_scope == scope:
return False
for en in scope.enums.values():
if en.file_scope == scope:
return False
for f in scope.functions:
if f.file_scope == scope:
return False
for st in scope.structures.values():
if st.file_scope == scope:
return False
for ct in scope.type_aliases.values():
if ct.source_mapping and ct.source_mapping.filename == scope.filename:
return False
for uf in scope.using_for_directives:
if uf.file_scope == scope:
return False
for v in scope.variables.values():
if v.file_scope == scope:
return False
return True
Copy link

Choose a reason for hiding this comment

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

The implementation of _is_import_container method looks robust and well-documented.

Consider adding unit tests to ensure this method accurately identifies import containers under various scenarios.

Would you like me to help by writing some unit tests or opening a GitHub issue for this task?

@0xalpharush 0xalpharush merged commit 5443292 into dev Jun 5, 2024
83 checks passed
@0xalpharush 0xalpharush deleted the fix/unused-import-fp branch June 5, 2024 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant