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

12 次查看(过去 30 天)
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 个评论
Alejandro Arrizabalaga
编辑:Alejandro Arrizabalaga 2021-5-28
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 2024-8-4
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.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-5-28
编辑:Jan 2021-5-28
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 个评论
Ron Fredericks
Ron Fredericks 2024-8-4
编辑:Ron Fredericks 2024-8-4
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by