Repository navigation
Conversation
|
|
||
| // None is subtype of every other type | ||
| if (Count == 0) | ||
| if (IsEmpty) |
There was a problem hiding this comment.
@krwq does this property resembles the same as the previous expression?
There was a problem hiding this comment.
it doesn't really look like tests the same thing, i would revert and suppress the warning
| } | ||
|
|
||
| Debug.Assert(sequence.Length > 0); | ||
| Debug.Assert(!sequence.IsEmpty); |
There was a problem hiding this comment.
@stephentoub could ReadOnlySequence<T> be another type that we may need to exclude?
| { | ||
| var self = this; | ||
| if (self.Length == 0) | ||
| if (self.IsEmpty) |
There was a problem hiding this comment.
These changes have potential to introduce performance regression due to shared generic code inlining limitations in .NET Framework. I think you would want to manually inline ImmutableArray.Length into ImmutableArray.IsEmpty to make sure that the new code is as good as the old one.
There was a problem hiding this comment.
ImmutableArray<T>.IsEmpty already inlines Length == 0; is that what you are referring to by "manually inline"?
There was a problem hiding this comment.
TestA shows the code that people will get today.
TestB shows the significantly slower code that people will get with this change.
TestC shows how you need to change IsEmpty property to fix this regression
There was a problem hiding this comment.
Cool; seems to me that that is the only place that needs to be fixed.
Thanks for the suggestion.
Btw, it means that until now, IsEmpty was performing worse than Length == 0 for ImmutableArray<T>?
Is there any negative repercussions given the NonVersionable attribute?
There was a problem hiding this comment.
Yep, in certain situations.
It suggests that we may want to exclude ImmutableArray<T> from this analyzer/fixer. Switching from Count to IsEmpty does not provide any benefit, and it can be even actively harmful for existing versions of the package.
There was a problem hiding this comment.
Switch to an allow list (referring back to our previous candidate list)
@jeffhandley Would it be reasonable to take a middle ground for the proposal so that allow list includes all types that implement one or more of the following interfaces: ICollection, ICollection<T> or IReadOnlyCollection<T>? I doubt if any collection type implementing these interfaces would have IsEmpty property that is not semantically equivalent to Count == 0.
There was a problem hiding this comment.
FWIW, XmlQueryType implements ICollection<T> indirecrly.
So, if we add ICollection, ICollection<T> or IReadOnlyCollection<T> to the allow list, we would still have #39890 (comment) as a false positive.
There was a problem hiding this comment.
IMO a type implementing any collection type such that .IsEmpty returns a different result then .Count == 0 has a serious API bug that deserves a suppression in source (for not well known types) OR explicit addition to analyzer's excluded list (for well-known runtime types with this issue) in addition to an API bug filed on the API owner.
Any case, if we are really concerned about semantics change by going from Count check to IsEmpty check for any possible code out there, going conservative with a much smaller explicit set of allowed types is the only option.
There was a problem hiding this comment.
Thanks, @mavasani. I agree from a library perspective, and the Xml example here is indeed a misleading API. With IsEmpty being loose without any interfaces that define its semantics, I do think the conservative approach is appropriate here. If there was an interface that included IsEmpty and we could limit to ICollection types that also use the interface defining IsEmpty, I think we'd be able to do this globally.
There was a problem hiding this comment.
without any interfaces that define its semantics,
There's a nice quote for that:
I often hear people saying that interfaces specify
contracts. I believe this is a dangerous myth. Interfaces, by themselves,
do not specify much beyond the syntax required to use an object.
-Krzysztof Cwalina (Annotation), Framework Design Guidelines
That said, I think it's reasonable to say that ICollection being in the type's hierarchy does give a strong expectation to an IsEmpty; so perhaps the configuration option approach that Manish suggested would be either "known good types" or "any ICollection types" and maybe a third mode of "any type with both IsEmpty and Count".
|
Closing in favor of #40629. |
| { | ||
| [NonVersionable] | ||
| get { return this.Length == 0; } | ||
| get { return this.array!.Length == 0; } |
There was a problem hiding this comment.
Could you please also submit this fix (in same or different PR)?
Follow-up to dotnet/roslyn-analyzers#3876
I ran the rule again now that it no longer reports for types
Memory,Span,ReadOnlyMemory&ReadOnlySpan.