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

[java] AvoidDeeplyNestedIfStmts violations can be unintentionally undetected #1499

Open
HoushCE29 opened this issue Dec 3, 2018 · 5 comments
Labels
a:false-negative PMD doesn't flag a problematic piece of code

Comments

@HoushCE29
Copy link

HoushCE29 commented Dec 3, 2018

Affects PMD Version: 5.3.3, 6.*, 7.0.0

Rule: design/AvoidDeeplyNestedIfStatements

Description: This rule has strange behavior where if you dispatch from an else block from somewhere else to a method containing violating deeply nested if code, it doesn't get caught as a violation. But if you do a simple if block but no else block, then it gets caught as a violation.

It's hard to explain without writing a book.... so see the sample code...

Code Sample demonstrating the issue:

This is a sample violating method:

private String doSomething(String somethingToDo) {
    if (condition) {
        if (anotherCondition) {
            if (problemDepthCondition) {
                return SOMETHING;
            }
        }
    }
    return SOMETHING_ELSE;
}

Here is code that the violation is caught for:

public String performSomething(String action) {
    if (action.equals(NOTHING)) {
        return NOTHING;
    }
    return doSomething(action);
}

However, this code (which is effectively the same) allows the violation:

public String performSomething(String action) {
    if (action.equals(NOTHING)) {
        return NOTHING;
    }
    // Only difference: using an else block
    else {
        return doSomething(action);
    }
}

Sure, the nested if's can and should be avoided and rewritten, but I would expect that if someone did code that way (which is what happened and why I am bringing up this issue) that it would be caught, even in the else block...

Please let me know if this actually isn't an issue (and is expected behavior), or if further clarification is needed.

Running PMD through: Gradle

@jsotuyod
Copy link
Member

jsotuyod commented Dec 3, 2018

@HoushCE29 thanks for the report!

You seem to be using a really old PMD version (latest version is PMD 6.9.0, with 6.10.0 upcoming). Can you please double check the problem persists under 6.9.0?

@HoushCE29
Copy link
Author

@jsotuyod Will do! I'll get back to you in the next week.

@HoushCE29
Copy link
Author

@jsotuyod Confirmed, it does in fact do that same behavior with version 6.9.0.

Do you require a copy of my "test-case" mini Gradle project for further evaluation?

@jsotuyod jsotuyod added the a:false-negative PMD doesn't flag a problematic piece of code label Dec 10, 2018
@jsotuyod
Copy link
Member

@HoushCE29 shouldn't be necessary, thanks!

@adangel
Copy link
Member

adangel commented Dec 10, 2018

It seems to be related to https://sourceforge.net/p/pmd/bugs/44/ - I was wondering, whether we always excluded somehow the else-branch. And we do this pretty long now (since 2002 - 7e4cdbd).
I agree, that adding an else, should not silence the rule...

@jsotuyod jsotuyod added needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale and removed needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale labels Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-negative PMD doesn't flag a problematic piece of code
Projects
None yet
Development

No branches or pull requests

3 participants