Question

I would like a resharper pattern to detect unhandled IDisposables if possible. If I have a method

 IDisposable Subscribe(...){....}

and call it without assigning and using that IDisposable I would like to be told about it. I have tried the following pattern

;$expr$;

where expr is of type IDisposable. The following happens.

enter image description here

the first is detected correctly but the second is an error because simple assignment to an existing variable is also and expression in C# whereas assignment using var is not. Is it possible to detect that the return value is assigned via structural search?

I notice that resharper has the following code quality options

enter image description here

but I'm guessing they are built with something more sophisticated than the structural search parser.

Was it helpful?

Solution

Unfortunately, this can't be done with structural search and replace. For one thing, there is no construct to match against the absence of something, so there's no way to match against a method invocation that does NOT have an assignment of its return value.

As you note, there are inspections that track pure functions that don't use the return value, and they're not implemented with SSR. You can make them apply to your methods by applying the [Pure] attribute to them. However, this is implying that the method actually is pure, i.e. has no side effects, so may be the wrong semantic in this instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top