Prevent mlint warning for onCleanup like return value

4 ビュー (過去 30 日間)
tommsch
tommsch 2024 年 7 月 2 日
コメント済み: Umar 2024 年 9 月 1 日
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?
  4 件のコメント
tommsch
tommsch 2024 年 8 月 23 日
What is the worth of that answer. You just repeated my question and all the stuff I know. Are you a ChatGPT bot? You may consider to delete your answer to reduce spam - I will delete my reply then too.
Umar
Umar 2024 年 8 月 24 日

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

https://www.mathworks.com/help/matlab/ref/checkcode.html

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2024 年 8 月 23 日
編集済み: Steven Lord 2024 年 8 月 24 日
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.
  5 件のコメント
tommsch
tommsch 2024 年 8 月 31 日
You still did not answer my question. So, lets just close this discussion.
Umar
Umar 2024 年 9 月 1 日
Hi @tommsch,
I do admit not answering your question directly and I am aware of inheriting from handle does allow for automatic resource management which can introduce overhead due to object reference counting. But exchanging ideas back and forth and finding out mlint is not recommended by mathworks documentation will help others when they will try to figure out the same problem as you encountered now in the future. End of discussion.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by