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

CA1416 Fix Version comparison ambiguity #4943

Merged
merged 1 commit into from
Apr 20, 2021

Conversation

buyaa-n
Copy link
Member

@buyaa-n buyaa-n commented Mar 11, 2021

Fixes #4932
The comparison operations of the Version type is a bit strange:

Console.WriteLine(Version.Parse("10.0.17763.0") < Version.Parse("10.0.17763")); // False
Console.WriteLine(Version.Parse("10.0.17763.0") > Version.Parse("10.0.17763")); // True
Console.WriteLine(Version.Parse("10.0.17763.0") == Version.Parse("10.0.17763")); // False

Because of that now [SupportedOSPlatform("windows10.0.17763.0")] is not equal to [SupportedOSPlatform("windows10.0.17763")] because Version.Revision will be defaulted to -1.
Accordingly API with [SupportedOSPlatform("windows10.0.17763.0")] attribute cannot be guarded with OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17763)

Added extension method for version comparison

@buyaa-n buyaa-n requested a review from a team as a code owner March 11, 2021 22:54
@codecov
Copy link

codecov bot commented Mar 11, 2021

Codecov Report

Merging #4943 (eeaa073) into release/6.0.1xx-preview3 (940c426) will increase coverage by 0.00%.
The diff coverage is 97.05%.

@@                    Coverage Diff                     @@
##           release/6.0.1xx-preview3    #4943    +/-   ##
==========================================================
  Coverage                     95.57%   95.57%            
==========================================================
  Files                          1175     1176     +1     
  Lines                        269300   269402   +102     
  Branches                      16314    16323     +9     
==========================================================
+ Hits                         257377   257494   +117     
+ Misses                         9804     9787    -17     
- Partials                       2119     2121     +2     

Copy link
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

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

There are still few comparisons left:

csAttribute.UnsupportedFirst != null && csAttribute.UnsupportedFirst > supportedVersion))

if (unsupportedVersion != null && unsupportedVersion > supportedVersion)

if (attribute.UnsupportedFirst == null || attribute.UnsupportedFirst > attribute.SupportedFirst)

(and few more)

Was that intended?

Copy link
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

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

Is there a way to ban the comparison operators for Version?

@buyaa-n
Copy link
Member Author

buyaa-n commented Mar 15, 2021

There are still few comparisons left:
...
Was that intended?

Yes the comparisons with > or < has no issue, only related to >= or <=

Is there a way to ban the comparison operators for Version?

That should be runtime related question, anyway, i don't think so because I heard there was a long discussion (idk when/where though) about the Version comparisons and if this behavior is correct or should be fixed etc, and ended up not fixing. I guess it didn't fix it because the existing behavior was made sense for some other scenarios

@Youssef1313
Copy link
Member

That should be runtime related question

I meant using the BannedApiAnalyzer to warn if we use >= or <= for Version. If there is (currently) no way for BannedApiAnalyzer to ban operators, it may be a reasonable feature request.

@mavasani @Evangelink Do you know whether BannedApiAnalyzer supports this?

@mavasani
Copy link
Member

Do you know whether BannedApiAnalyzer supports this?

Yes, we can ban usage of specific APIs in NetAnalyzers projects by adding the documentation comment IDs of the APIs to https://github.com/dotnet/roslyn-analyzers/blob/dc515b0412abf13d9d2b8e34543c61c9abbee87b/src/NetAnalyzers/BannedSymbols.txt

Copy link
Member

@carlossanlop carlossanlop left a comment

Choose a reason for hiding this comment

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

@buyaa-n I left a couple of questions.

Can you please address @Youssef1313 's question about the other comparisons that were not modified?

Also, let's try to explore the suggestion to ban <= and >= for Version instances. Maybe we can find a way to ban them only inside the PlatformCompatibilityAnalyzer class.

@buyaa-n
Copy link
Member Author

buyaa-n commented Mar 18, 2021

Can you please address @Youssef1313 's question about the other comparisons that were not modified?

That question is answered here, in short, < or > comparisons unrelated, only >= or <= related to this change

Also, let's try to explore the suggestion to ban <= and >= for Version instances. Maybe we can find a way to ban them only inside the PlatformCompatibilityAnalyzer class.

From what I have seen and what @mavasani answered seems we cannot do that for only PlatformCompatibilityAnalyzer, correct me if I am wrong, i am in doubt if we should do that for the entire NetAnalyzers project, existing Version comparisons might be useful for other analyzers

Copy link
Member

@carlossanlop carlossanlop left a comment

Choose a reason for hiding this comment

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

LGTM. All questions answered.

@buyaa-n buyaa-n changed the base branch from release/6.0.1xx-preview3 to release/6.0.1xx-preview4 April 19, 2021 22:43
@buyaa-n
Copy link
Member Author

buyaa-n commented Apr 20, 2021

Followed up with @mavasani, we cannot ban version comparison only for PlatformCompatibilityAnalyzer, i don't think we should ban Version comparison in the entire project, so gonna merge the PR

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

Successfully merging this pull request may close these issues.

None yet

4 participants