S1854: False-negative after try-except

Language: Python 3.10
Rule: S1854 - Unused assignments should be removed
IDE: IntelliJ 2024.2.3 with Sonarlint Plugin 10.10.0.79575

The linter do not take this rule into account when there is a try-except block:

def example():
    try:
        do_something = "hello world"
    except ValueError:
        pass
    
    if "this" == "that":
        test = "some value"
    
    test = "other value"
    
    print(test)

Removing the try-except block will correctly identify the issue with the assignment.

Hello @Ort,

Thank you for reporting this. We are aware of this limitation in the analyzer, which is due to a limitation in the modelization of try/except itself.
We plan to have a deeper look at this in the near future, but I cannot provide a timeline for a fix.

Cheers,
Guillaume

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.