Syntax for ignoring Code Analyzer warning "This statement cannot be reached"

41 views (last 30 days)
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?
  1 Comment
Alejandro Arrizabalaga
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.

Sign in to comment.

Accepted Answer

Jan
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
...

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!