This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 254445

Summary: It should be possible to suppress 'The if statement is redundant'
Product: java Reporter: tln
Component: HintsAssignee: Svata Dedic <sdedic>
Status: NEW ---    
Severity: normal CC: matthies, mkhramov, supuhstar, TradeIdeasPhilip
Priority: P3    
Version: 8.0.2   
Hardware: PC   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:

Description tln 2015-08-17 15:45:42 UTC
In many cases 'The if statement is redundant' message is good advice, but there are some cases where the code is MUCH clearer by intentionally using the redundant 'if'.

That's why it should be possible to suppress the warning using @SuppressWarnings for these cases in order not to have to disable the warning globally. It has its merits and should be enabled in general.

Example for a case where suppressing the warning would be appropriate and best choice in my opinion:


public boolean matches(Item item)
{
   if (!doesDetailMatch(item.getDetail1(), 'a')) return false;
   if (!doesDetailMatch(item.getDetail2(), 'b')) return false;
   if (!doesDetailMatch(item.getDetail3(), 'c')) return false;
   if (!doesDetailMatch(item.getDetail4(), 'd')) return false;

   if (!doesDetailMatch(item.getDetail5(), 17)) return false;
   if (!doesDetailMatch(item.getDetail6(), 20)) return false;
   if (!doesDetailMatch(item.getDetail7(), -5)) return false;

   return true;
}


In this example the last 'if' statement is marked as redundant, which is technically correct, but in this kind of case it's obvious why you want to keep it that way. Please make the warning suppressable by annotation.

Thank you.

PS: Other posters have pointed out similar examples, just in a different contexts, for example #247780 and #238286 (at the end). I think they'd also appreciate the feature. Thanks again.
Comment 1 Svata Dedic 2015-10-05 11:01:40 UTC
issue #247780 focuses on a different problem. Trivial, the hint impl only does not declare suppress warnings code.
Comment 2 matthies 2016-03-11 23:31:01 UTC
Voted, please do implement this. I'm always mystified why not all hints get assigned a suppression ID by default. For every warning, there are circumstances where you want to suppress it for a particular piece of code where you know what you're doing or where the general rule does not apply.
Comment 3 supuhstar 2016-03-19 20:31:01 UTC
Voted. Sometimes, I want my code to be as terse as possible for efficiency, and sometimes I value explicit human-readability over terseness. I want the Java compiler to work with that want.
Comment 4 supuhstar 2016-03-19 20:37:55 UTC
*** Bug 238286 has been marked as a duplicate of this bug. ***