How does polyspace bug finder intelligently identify useless if errors in if-else
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Shubham
2023-9-6
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 个评论
Shubham
2023-9-6
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spline Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!