1

We found a bug in old code which could be easily found if there would be a warning,

The issue was that inner class member was used but not assigned or initialized. Sample code minimized. Problem is that url is always null.

 public class Main {
    public class Sender {
       private String url;

        public Sender() {
         }
         public void send() {
             OtherClass.send(url);
         }
      }
   }

In intellij it warns about variable never assigned. In eclipse I didn't find such warning.

The code is more complex and adding a warning at any level will reduce time to find similar bugs.

Is it possible in eclipse to warn in some way if variable is used before assigned/initialized ?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233

1 Answers1

2

You can use SpotBugs(successor of findbugs) eclipse plugin. Right click on project Spotbugs->find bugs this will find these types of bugs.

I suggest also installing sonarlint plugin which has good static analysis capabilities.

https://marketplace.eclipse.org/content/spotbugs-eclipse-plugin

https://marketplace.eclipse.org/content/sonarlint

miskender
  • 7,460
  • 1
  • 19
  • 23
  • SpotBugs didn't find the issue, should I configure it and how? – Ori Marko Mar 04 '18 at 06:51
  • @user7294900 I copied your sample code, replaced otherclass.send with system.out.print method. It does find it at system.out.print line. I am using oxygen 1.a and latest SpotBugs – miskender Mar 04 '18 at 07:38
  • does it warn you about `private String url` ? been used without initiialized – Ori Marko Mar 04 '18 at 08:11
  • @user7294900 after Project selected and Right click Spotbugs->find bugs, there is view named `Bug explorer` in there it is listed as `This field is never written. All reads of it will return the default value. Check for errors (should it have been initialized?), or remove it if it is useless.` You can open this view window -> show view-> other view and search for bug explorer. And in the source code there is a small bug icon near that line – miskender Mar 04 '18 at 09:19
  • @user7294900 I just relized your send method takes parameter url, where does it called from. if it is called from outside, it wont use the url in the Sender class. – miskender Mar 04 '18 at 09:20