Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disposable analyzers are not CompositeDisposable aware #3477

Open
Evangelink opened this issue Apr 3, 2020 · 6 comments
Open

Disposable analyzers are not CompositeDisposable aware #3477

Evangelink opened this issue Apr 3, 2020 · 6 comments

Comments

@Evangelink
Copy link
Member

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

v3.0.0-beta3.final (latest pre-release)

Diagnostic ID

Example: CA2213

But I assume some other analyzers might be wrong too.

Repro steps

public class MyObservableObject
{
	private readonly CompositeDisposable _disposables = new CompositeDisposable();
	private readonly Subject<bool> _canUndo;

	public MyObservableObject()
	{
		_canUndo = new Subject<bool>();
        _disposables.Add(_canUndo);

		// or

		_canUndo = new Subject<bool>().DisposeWith(_diposables);
	}
}
@Evangelink
Copy link
Member Author

@mavasani I'd be happy to be able to have a chat with you some time about introducing some specific attributes that would help the analyzers to have a more accurate behavior. See #3174

@Evangelink
Copy link
Member Author

@mavasani If we don't want to have a built-in "exception" for such case, would it be possible to at least create some kind of option where we could define a list of method doing some ownership transfer?

@Evangelink
Copy link
Member Author

I think it would be handy to have something a little more fine-grained that Analyzer Configuration.md#configure-dispose-ownership-transfer-for-disposable-objects-passed-as-arguments-to-method-calls and instead have a list of methods/types/namespaces that are considered as transferring ownership.

@mavasani WDYT?

@mavasani
Copy link
Contributor

mavasani commented Aug 7, 2020

I think we need to go with attribute annotation based solution here, similar to nullable attributes. editorconfig option might provide an alternate, but attributes are much more descriptive in source. @Evangelink would you like to come up with a proposal about set of attributes that we can use here?

@Evangelink
Copy link
Member Author

I think we could have a look at this proposal: dotnet/runtime#29631

@manfred-brands
Copy link
Contributor

There is an inconsistency between CA2000 and CA2213.

CA2000 recognizes "Collection Add methods" as ownership transfer.
See

CA2213 does not or do I need to say not anymore.
Our code use field = Disposer.Add(new DisposableClass()).
When compiling against version 6.0.0 of the analyzers, I get no warning from 7.0.0 onwards I do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants