Syntax for ignoring Code Analyzer warning "This statement cannot be reached"
25 views (last 30 days)
Show older comments
I'm aware of a few warning ignore comment syntaxes, such as
%#ok<AGROW>
for variables that change size on each loop iteration. But I haven't been able to find one for if branch errors, such as in
flag = 0;
if flag
foo
end
In fact, it's been stated elsewhere that no central list of warning IDs exists, which frankly is inexcusable. Where is a list of common warning IDs and their ignore comments?
2 Comments
Alejandro Arrizabalaga
on 28 May 2021
Edited: Alejandro Arrizabalaga
on 28 May 2021
I used to suffer a lot from this issue. The solution is very simple but also weird, which is just to do:
flag = 0;
if flag ~= 0
foo
end
I would have expected MATLAB to check flag to be true (1) or false (0) without having to explicitly write flag ~= 0. But it seems that MATLAB Code Analyzer complains if one does not do this.
Ron Fredericks
on 4 Aug 2024
Alejandro's suggested solution is the best for my code use - as even if there is an "else" clause in the if statement there is no useless warning produced.
Accepted Answer
Jan
on 28 May 2021
Edited: Jan
on 28 May 2021
Simply press the right mouse key on the line, which is marked in the editor. Then "supressing on this line" inserts (at least in R2018b):
%#ok<UNRCH>
I find a list at:
C:\Program Files\MATLAB\R2018b\help\matlab\matlab_mlint_csh\matlab_mlint_csh.map
% ^^^^^^ adjust this
mlint_nospc the-file-file-is-too-large-or-complex-to-analyze-nospc.html
mlint_mbig this-file-is-too-big-for-code-analyzer-to-handle-mbig.html
mlint_badot use-of-two-dots-is-an-invalid-matlab-construction-badot.html
mlint_mdeep parentheses-brackets-and-braces-are-nested-too-deeply-mdeep.html
mlint_deepc block-comments-are-nested-too-deeply-deepc.html
mlint_deepn functions-are-nested-too-deeply-deepn.html
...
mlint_unrch this-statement-and-possibly-following-ones-cannot-be-reached-unrch.html
...
You can produce the list dynamically also: Open Matlab's preferences -> Code Analyser -> select one entry -> Ctrl-A -> "Disable all" -> Save as file with a new file name -> Restore the former settings.
Now open the file in the prefdir folder:
# Copyright 2021 The MathWorks, Inc.
0ADAPPREF
0ADMTHDINV
0ADPROP
0ADPROPLC
0ADTPATH
0AFADJLMS
...
2 Comments
Ron Fredericks
on 4 Aug 2024
Edited: Ron Fredericks
on 4 Aug 2024
The problem with Jan's proposed solution is that when the flag logic is flipped and there is an else clause, new suppression clicks have to be made. While Alejandro's suggestion using "~=" continues to work if and else clause is added or flogic lag is flipped.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!