How does polyspace bug finder intelligently identify useless if errors in if-else
10 views (last 30 days)
Show older comments
The if-ESLE condition in the source code is the user's input, and according to the input value, it is decided which if branch to execute. The condition will have an initial value, and the bug finder will conclude that all other branches that do not satisfy the condition are useless if.How to avoid not reporting a useless if error in this case
For example:
#define value 5
int main(){
if (value == 5 )
printf("if condition")
else:
printf("useless if") // bug finder will determine that this branch is useless if
}
}
0 Comments
Answers (1)
Shubham
on 6 Sep 2023
Hi,
I understand you want to avoid the useless check flagged by Polyspace.
The reason Polyspace is marking the variable 'value' as useless is because you have used the 'define' macro to fix its value as 5. By definition, the 'define' macro assigns a constant value to a variable, which cannot be changed during execution. Therefore, Polyspace considers 'else' as useless because it will never be executed because the value of variable 'value' will always be 5.
If you want 'val' to be a user input that can vary during execution, then you should not use a macro to assign its value. Instead, you can use a different approach to allow the user to input the value of 'val' dynamically, such as using the 'scanf' function.
I hope this clarifies the reason behind Polyspace marking 'else' as useless.
2 Comments
Shubham
on 6 Sep 2023
Hi, you can use your preffered way of taking an input, but you should avoid "#define" to assign the value if it will be changed during execution.
Additionally Polyspace does not allow customization of defect rules but you can suppress the acceptable defects by adding code annotations. Check this documentation for more info:Annotate Code and Hide Known or Acceptable Results - MATLAB & Simulink (mathworks.com)
See Also
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!