Prevent mlint warning for onCleanup like return value
42 次查看(过去 30 天)
显示 更早的评论
I wrote a function, similar to onCleanup. I noticed that Matlab does not give a mlint warning for the following code.
dummy = onCleanup( @() some_func );
But for my own function
dummy = MyOwnCleanup( @() some_func );
I get the warning Value assigned to variable might be unused, which I need to silence with %#ok<NASGU>. Obviously, Matlab recognizes the onCleanup call and does not emit a warning. How can I acchieve similar behaviour for my own MyOwnCleanup function?
5 个评论
Umar
2024-8-24,4:05
Hi @ tommsch,
You mentioned,” You just repeated my question and *all the stuff I know* “
Now, in your posted comments, you mentioned,
“I wrote a function, similar to onCleanup. I noticed that Matlab does not give a mlint warning for the following code. dummy = onCleanup( @() some_func );”
Could you please click the mathworks mlint documentation link below and tell me what does it say about mlint
回答(1 个)
Steven Lord
2024-8-23,20:52
编辑:Steven Lord
about 21 hours 前
So I wrote a cleanup function which accepts parameters, so you can write
cleandir = onCleanup_lazy( @(x) cd( x ), pwd );
Be careful with how you implement this if some of the parameters are state-dependent. What you've written here is fine, as pwd will be evaluated when onCleanup_lazy is called and the value returned by that function is what would be captured. I assume onCleanup_lazy is implemented something like:
function oc = onCleanup_lazy(fh, varargin)
fh2 = @() fh(varargin{:});
oc = onCleanup(fh2);
end
But consider if you'd written your onCleanup_lazy call slightly differently.
cleandir = onCleanup_lazy( @() cd(pwd));
This wouldn't do what you wanted, as pwd would only get called when the onCleanup object returned by onCleanup_lazy was being destroyed. This is likely well after you changed away from the directory that was pwd when you called onCleanup_lazy, and so that onCleanup object would have done nothing.
Getting back to your original question, I believe onCleanup is handled specially by Code Analyzer. Code Analyzer "knows" (aka we told Code Analyzer) that onCleanup objects generally aren't going to be interacted with in code after they were created. [Occasionally, if you want to control exactly when their tasks are executed, you may want to delete them, but that's about it.] Therefore we exempted it from the "This variable may be unused" check. I don't believe there is a way for users to instruct Code Analyzer that it should exempt their objects from this check as well. You could contact Technical Support and request a way to specify in the definition of a function or class that uses/instances of this function or class should not issue this Code Analyzer warning.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!