[SonarJava] New Rule Request - Declaration can have final modifier

29 views
Skip to first unread message

Marcel

unread,
Apr 19, 2017, 5:11:33 PM4/19/17
to SonarQube, Marcel Swoboda

Hi,


I miss a rule in SonarJava which checks if a local field can be final. I know this rule from the Intellij IDEA (Declaration can have final modifier inspection).

Is there a reason why this rule does not exist? If not, it would be great if you could implement this rule.


Regards

Marcel



public class App 
{
    private List<String> collection = new ArrayList<>(); // should be final
    private Collection<String> items; // should be final

    public App(Collection<String> items)
    {
        this.items = items;
    }

    public void foo()
    {
        collection.add("Foo");
    }

    public void bar()
    {
        collection.stream().forEach(System.out::println);
        items.stream().forEach(System.out::println);
    }
}



G. Ann Campbell

unread,
Apr 20, 2017, 9:06:11 AM4/20/17
to SonarQube, marcel....@gmx.de
Hi Marcel,

We have two related rule specifications for this:
RSPEC-3353 Unchanged local variables should be "final"
RSPEC-2108 Fields that are never updated should be constant

They're just waiting to be picked for implementation.


Ann
Reply all
Reply to author
Forward
0 new messages