Generate mlint warning when variable is not unused

5 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 30 Aug 2024
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 on 30 Aug 2024
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;

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 30 Aug 2024
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 Comments
Walter Roberson
Walter Roberson on 30 Aug 2024
Edited: Walter Roberson on 30 Aug 2024
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.

Sign in to comment.

More Answers (1)

Jatin
Jatin on 30 Aug 2024
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 Comments
tommsch
tommsch on 30 Aug 2024
Dear Jatin, please understand the question before you post an answer.
Jatin
Jatin on 30 Aug 2024
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.

Sign in to comment.

Categories

Find more on Programming Utilities in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!