fluentassertions / fluentassertions.analyzers

Analyzers based on the FluentAssertions best practices docs
https://www.fluentassertions.com/tips
MIT License
137 stars 21 forks source link

Misleading diagnostic and wrong fix for array.Length.Should().Be() #242

Closed petterh closed 1 year ago

petterh commented 1 year ago

Description

Misleading diagnostic and wrong fix when asserting array length

Complete minimal example reproducing the issue

int[] ar = new int[2];
ar.Length.Should().Be(2);

Expected behavior:

I'm not sure what to expect. "Actual behavior" should make the problem clear.

Actual behavior:

The code triggers FluentAssertions0206: Use .Should().HaveLength() instead. This would be fine, except the only HaveLength I've seen applies to strings, not to arrays.

Applying the "Simplify Assertion" fix changes the code to:

ar.Should().HaveLength(2);

This does not compile, as ar is not a string.

If FluentAssertions were to support array Length properties with a HaveLength assertion, that would be nice. In lieu of that, HaveCount would actually make sense here.

Versions

FluentAssertions 6.9.0
FluentAssertions.Analyzers 0.25.0
target .net6.0
Meir017 commented 1 year ago

@petterh fixed in release 0.26.0, thanks for reporting this!

petterh commented 1 year ago

@Meir017 Thanks for fixing -- impressive turnaround!