Generate mlint warning when variable is not unused

조회 수: 4 (최근 30일)
tommsch
tommsch 2024년 8월 30일
댓글: tommsch 2024년 9월 1일
mlint gives a warning when a variable is unsed. E.g. in the following code, mlint gives a warning in line (1)
a = 2; % (1) - here mlint gives a warning (which I know why, and which I do not care about)
a = 3; % (2)
Is it possible to generate a warning when a variable name is not used. More exactly, I would like to have the following behaviour:
a = 2; % (3) - I want no warning here, because the variable name is used again in line (4) and/or line (5)
a = 3; % (4)
func( a ); % (5)
a = 2; % (6) - I want a warning here, because the variable name is not used again later on in all paths
if( some_condition );
a = 3;
end
  댓글 수: 5
Walter Roberson
Walter Roberson 2024년 8월 30일
a = 1;
fid = fopen('test.txt', 'w');
if fid > 0
fprintf(fid, '%d\n', a);
fclose(fid);
end
and in this case you would want a = 1 to be flagged because the fopen might fail so the fprintf() might not be called?
tommsch
tommsch 2024년 8월 30일
Yes. But I am already satisfied when it works not across function call boundaries, i.e. no difference in behaviour for the variables a and b here. (Since mlint quite surely does not analyse the called functions for its warning, I doubt this would be possible at all).
So, if I inline all the functions from your example, the behaviour should be:
a = 1; % warning, because it is not used anymore
b = 2; % no warning, because it is used later
c = 3; % warning, because it is only conditionally used later
b = b
if rand() > 0.5;
c;
end;

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2024년 8월 30일
Is it possible to generate a warning when a variable is not used. More exactly, I would like to have the following behaviour:
a = 2; % (3) - I want no warning here, because the variable is used again in line (4) and/or line (5)
a = 3; % (4)
func( a ); % (5)
It is possible to suppress the Code Analyzer warning on the line labeled 3 as Jatin suggested. But I'd like you to clarify what you mean by "used" in your comment.
The variable name a is assigned to on the lines labeled 3 and 4 and referenced on the line labeled 5. But the value that you set on the line labeled 3 is not used. That's the whole point of the message.
Suppose that instead of assigning the value 2 to a on that line you assigned the result of a computation that took 5 minutes to a on that line. You then turn around and throw that value that MATLAB spent 5 minutes working on in the trash and stick the value 3 in its place. That's potentially a waste of 5 minutes, and that's why Code Analyzer warns you on the line labeled 4. Code Analyzer is basically asking you "Did you really mean to throw away that work I did? Or did you perhaps mistype the variable name on one of the lines?"
In this case, 2 doesn't take any time to compute. But it's the principle: the fact that you assigned data to a variable name then immediately discarded it suggests that you may not be doing what you intended.
  댓글 수: 7
Walter Roberson
Walter Roberson 2024년 8월 30일
편집: Walter Roberson 2024년 8월 30일
If you happen to be working with single or double,
function [ xx ] = func()
xx = nan; % fallback value, if something goes wrong so that no exception is thrown
if( condition1 )
xx = 2;
% very complicated if things continue
xx = 3;
% very complicated if things continue
elseif( condition2 )
xx = 4;
if( condition4 )
xx = 5;
elseif( condition3 )
% very complicated if things continue
xx = 6;
% very complicated if things continue
xx = 7;
% very complicated if things continue
% very complicated if things continue
% very complicated if things continue
end
end
if isnan(xx)
fprintf("Failed to assign to xx\n");
end
end
This is a dynamic warning rather than a static warning, so it will not be triggered until the first time you happen to miss an assignment.
tommsch
tommsch 2024년 9월 1일
Thanks for this nice idea.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jatin
Jatin 2024년 8월 30일
The warning message about “Value assigned to variable might be unused” at line 3 in your example is becausemlintworks by finding variables that are defined but not used before being overwritten or the script ends.
In MATLAB “mlint” does not provide a way to customize warnings as you describe, but you can suppress warning messages in MATLAB scripts using few ways described below:
1. You can add “%#ok<NASGU>” at the line with warning to suppress “Value assigned to variable might be unused.” warning message, see example below:
function a = mlintcheck()
c = 2; %#ok<NASGU>
b = 5;
if(b > 1)
c = 1;
end
end
2. Using User Interface: Right click on the underline of warning message and select: Suppress Message >> On This Line.
In the provided example, line 6 assigns a value to the variable a, but it does not use a afterward, which should typically trigger a warning about the variable being unused. However, if the variable a were to be used as a return value from a function or passed to another function later, the script might not generate this warning.
Note: MATLAB recommends using “checkcodein place of “mlint” which gives better integration with MATLAB’s Code Analyzer.
Kindly refer to the documentation below to know more about “mlint” and “Adjust Code Analyzer Message Indicators and Messages”:
  댓글 수: 2
tommsch
tommsch 2024년 8월 30일
Dear Jatin, please understand the question before you post an answer.
Jatin
Jatin 2024년 8월 30일
Dear tommsch, I attempted to clarify your questions based on the comments you provided in the code. I recommend framing your questions in more detail for better understanding.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming Utilities에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by