简体   繁体   中英

Eclipse Errors/Warnings ignore assert in unused variable analysis

I have had 'hidden' bugs due to Eclipse not reporting 'unused' variables because they were used in an assertion (precondition), for example:

public void method(final String text, ...other parameters) {
  assert StringUtils.isNotBlank(text);
  ...
  // The text variable is never used in the method.
  // If Eclipse had reported the variable as 'unused' I would have noticed that something is wrong with the code.
  ...
}

I would like to tell Eclipse to ignore assertions when checking for unused variables. I doubt anyone would pass a parameter to only run an assertion on it... Also let me know if FindBugs or another tool can do this.

Here is my own attempt at solving this with a 'brute' solution:

  • Write a script that makes a copy of your source code and removes all assertions from it
  • Inspect that assert less copy as you would inspect the original
  • Manually remove variables used only in asserts from the original source code

Note that there is a similar problem with JavaDoc linked variables... if a variable is mentioned in the JavaDoc it will be considered as used (by Eclipse and maybe other IDE).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM