Generate mlint warning when variable is not unused
Show older comments
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
Stephen23
on 30 Aug 2024
"because the variable from line (3) is used"
How exactly is the variable from line (3) used ?
tommsch
on 30 Aug 2024
Walter Roberson
on 30 Aug 2024
a = 1;
b = 2;
c = 3;
myfun1(a)
myfun2(b)
myfun3(c)
function myfun1(x)
end
function myfun2(y)
y;
end
function myfun3(z)
if rand() > 0.5
z;
end
end
To clarify, you would want mlint to produce warnings for a=1 and c=3 but not for b=2 ? Because the call to myfun1(a) does not use the variable at all, and the call to myfun3(c ) only uses the variable sometimes?
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
on 30 Aug 2024
Accepted Answer
More Answers (1)
Jatin
on 30 Aug 2024
The warning message about “Value assigned to variable might be unused” at line 3 in your example is because “mlint” works 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 “checkcode” in 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”:
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!