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

25 Ansichten (letzte 30 Tage)
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 Kommentare
Alejandro Arrizabalaga
Alejandro Arrizabalaga am 28 Mai 2021
Bearbeitet: Alejandro Arrizabalaga am 28 Mai 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
Ron Fredericks am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 28 Mai 2021
Bearbeitet: Jan am 28 Mai 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 Kommentare
Jack Gallagher
Jack Gallagher am 2 Jun. 2021
as simple as a right-click! Thank you for the useful yet comprehensive answer.
Ron Fredericks
Ron Fredericks am 4 Aug. 2024
Bearbeitet: Ron Fredericks am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by