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

조회 수: 17 (최근 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일
편집: 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
Jack Gallagher
Jack Gallagher 2021년 6월 2일
as simple as a right-click! Thank you for the useful yet comprehensive answer.
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