Generate mlint warning when variable is not unused

17 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(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