How to suppress Code Analyzer that prior suppression is now un-needed
4 views (last 30 days)
Show older comments
I have a script where for debugging I have code that runs and needs some earlier generated variables to do so. When the debugging code block is commented out, these early variables need %#ok<NASGU> to suppress Code Analyzer. But when I uncomment the debugging code the %#ok<NASGU> is not needed and Code Anlyzer flags it with the message "A Code Analyzer message was once suppressed here, but the message is no longer generated".
I found within C:\Program Files\MATLAB\R2019a\resources\CodeAnalyzer\en\caBuiltins.xml there's the code for it, MSNU, but I can't seem to get it to work on just the lines I want.
Example: These 3 parameters are used in the debugging. Ingnoring any code correctness stuff, when the debugging portion is commented out, the NASGU is needed. When debugging is uncommented, the NASGU is not needed and it becomes highlighted by code analyzer. I can't get MSNU to work on a single line in this style. Image inserted here to capture error coloring.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/464975/image.png)
I know I have the syntax in a form that should work since the following line does fine to suppress 2 warnings.
axes_array{window_counter} = axes(figure_array{window_counter}); %#ok<LAXES,AGROW>
I did discover that I can suppress it for the whole file with a * in front of MSNU, but I like being told I no longer need a message suppression in the rest of my code - I just want to ignore it in the parts I know will cause issues. I also know that once I have *MSNU it doesn't matter the order in the <> that I put them, nor do I need the *MSNU call in that line, it can be anywhere in the file.
figure_array = {}; %#ok<*MSNU,NASGU>
axes_array = {}; %#ok<NASGU>
window_counter = 1; %#ok<NASGU>
So what I'm looking for is the appropriate syntax to use MSNU to suppress the "A Code Analyzer message was once suppressed here, but the message is no longer generated" message on a single line without suppressing it for the whole file.
1 Comment
Rik
on 18 Dec 2020
An intriguing problem. Personally I currently circumvent issues like this with the function below. That also allows me to trigger errors in carefully controlled places when running a tester function. I also tend to work with very small units of code, so it is easy to see where to add/remove mlint warnings.
function varargout=debug_hook(varargin)
%This function can be used to return several outputs (including warnings and errors), determined by
%the global variable HJW___test_suit___debug_hook_data.
%
%Every iteration the first element is removed from HJW___test_suit___debug_hook_data.
%
%When HJW___test_suit___debug_hook_data is empty or when returning a warning this functions returns
%the input unchanged.
global HJW___test_suit___debug_hook_data
if isempty(HJW___test_suit___debug_hook_data)
varargout=varargin;return
end
element=HJW___test_suit___debug_hook_data(1);
HJW___test_suit___debug_hook_data(1)=[];
switch element.action
case 'return'
varargout=element.data;
case 'warning'
varargout=varargin;
warning(element.data{:})
case 'error'
error(element.data{:})
case 'warning_'
varargout=varargin;
warning_(element.data{:})
case 'error_'
error_(element.data{:})
end
end
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!