Skip to content

Fix CA1836 rule violations excluding Memory/Span - #39890

Closed
jozkee wants to merge 2 commits into
dotnet:masterfrom
jozkee:fix_ca1836_2
Closed

jozkee wants to merge 2 commits into
dotnet:masterfrom
jozkee:fix_ca1836_2

Conversation

@jozkee

@jozkee jozkee commented Jul 24, 2020

Copy link
Copy Markdown
Member

Follow-up to dotnet/roslyn-analyzers#3876
I ran the rule again now that it no longer reports for types Memory, Span, ReadOnlyMemory & ReadOnlySpan.

@jozkee jozkee added this to the 5.0.0 milestone Jul 24, 2020
@jozkee
jozkee requested review from a team, bartonjs and stephentoub July 24, 2020 10:05
@jozkee jozkee self-assigned this Jul 24, 2020

// None is subtype of every other type
if (Count == 0)
if (IsEmpty)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krwq does this property resembles the same as the previous expression?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentoub could ReadOnlySequence<T> be another type that we may need to exclude?

{
var self = this;
if (self.Length == 0)
if (self.IsEmpty)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ImmutableArray<T>.IsEmpty already inlines Length == 0; is that what you are referring to by "manually inline"?

public bool IsEmpty
{
[NonVersionable]
get { return this.Length == 0; }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jozkee jozkee Jul 24, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mavasani mavasani Aug 5, 2020 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

@jozkee

jozkee commented Aug 10, 2020

Copy link
Copy Markdown
Member Author

Closing in favor of #40629.

@jozkee jozkee closed this Aug 10, 2020
{
[NonVersionable]
get { return this.Length == 0; }
get { return this.array!.Length == 0; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also submit this fix (in same or different PR)?

@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@jozkee
jozkee deleted the fix_ca1836_2 branch March 24, 2021 19:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants