Skip to content

[java] LawOfDemeter - false negative with braces #3801

@haoyuwang99

Description

@haoyuwang99

Affects PMD Version: 6.42.0

Rule:

LawOfDemeter https://pmd.github.io/latest/pmd_rules_java_design.html#lawofdemeter

Description: When there is no extra braces, PMD can alert LawOfDemeter correctly, while it fails to alert when adding a pair of braces as shown in the following code.

Code Sample demonstrating the issue:

public class Example {

    public void func() {
        MyObject myObject = MyObjectProvider.get();
        {myObject.func();} //false-negative here
    }
}

class MyObject {
    public void func(){
        // do nothing
    }
}

class MyObjectProvider {
    public static MyObject get(){
        return new MyObject();
    }
}

Expected outcome:

PMD should report a violation at line 5, but doesn't. This is a false-negative.

Running PMD through: [CLI]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions