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

CA1802: Don't report diagnostic for preview feature constant interpolated strings #4741

Merged
merged 4 commits into from
Apr 15, 2021

Conversation

Youssef1313
Copy link
Member

@Youssef1313 Youssef1313 commented Jan 27, 2021

Fixes #4732

@Youssef1313 Youssef1313 requested a review from a team as a code owner January 27, 2021 10:30
@@ -30,7 +30,7 @@
<!-- Roslyn -->
<MicrosoftCodeAnalysisVersion>3.3.1</MicrosoftCodeAnalysisVersion>
<MicrosoftCodeAnalysisForRoslynDiagnosticsAnalyzersVersion>3.7.0</MicrosoftCodeAnalysisForRoslynDiagnosticsAnalyzersVersion>
<MicrosoftCodeAnalysisVersionForTests>3.8.0</MicrosoftCodeAnalysisVersionForTests>
<MicrosoftCodeAnalysisVersionForTests>3.9.0-3.final</MicrosoftCodeAnalysisVersionForTests>
Copy link
Member Author

Choose a reason for hiding this comment

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

Version 3.8.0 doesn't produce operation tree where interpolated string has constant value. I had to update to be able to test.

Comment on lines 105 to 110
private static bool IsConstantInterpolatedStringSupported()
{
// TODO: When constant interpolated string is supported in a stable language version (most likely C# 10), this method should be updated.
// The feature is currently available for preview only.
return false;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't return true for preview version based on #4732 (comment)

When this is implemented later, it will need to be abstract with language-specific implementation.

A possible another approach would be to introduce a new service that gives information about features.

I'd love to be able to do the following (or similar):

var service = compilation.GetService<ILanguageFeatureSupportService>();
if (service.IsConstantInterpolationSupported(compilation)
{
    // do something.
}

@Youssef1313
Copy link
Member Author

@mavasani Test failure is for another analyzer CA1834. This happened because the operation tree changed after updating the compiler used in tests. Can you give it a look? Both behaviors can be reasoned about, so I don't know what will be your preference.


private static bool IsConstantInterpolatedStringSupported()
{
// TODO: When constant interpolated string is supported in a stable language version (most likely C# 10), this method should be updated.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you instead check the LanguageVersion from the Parse options and get rid of the TODO? You would need to add analyzer sub-types to C# and VB layers to access the parse options.

Copy link
Contributor

@mavasani mavasani left a comment

Choose a reason for hiding this comment

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

Lets address the TODO by checking the parse options.

@Youssef1313 Youssef1313 force-pushed the ca1802-fp-const-interpolated branch from d6eba7b to b02c297 Compare February 16, 2021 17:55
Copy link
Contributor

@mavasani mavasani left a comment

Choose a reason for hiding this comment

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

Thanks!

@mavasani
Copy link
Contributor

@Youssef1313 tests are failing

@Youssef1313
Copy link
Member Author

@mavasani Failing test is for another analyzer. Could you check what should be the correct behavior for it?

Base automatically changed from master to main March 5, 2021 02:20
@mavasani mavasani closed this Apr 13, 2021
@mavasani mavasani reopened this Apr 13, 2021
@mavasani
Copy link
Contributor

Failing test is for another analyzer. Could you check what should be the correct behavior for it?

@Youssef1313 You need to update the failing test to expect an analyzer diagnostic given that with the new MS.CA package for testing, interpolated string that is made up of constant constituent parts is also considered a constant now.

}
}
}";
await VerifyCS.VerifyAnalyzerAsync(interpolatedString_cs);

await VerifyCS.VerifyCodeFixAsync(interpolatedString_cs, interpolatedString_cs);
Copy link
Member Author

Choose a reason for hiding this comment

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

@mavasani No fix is offered for this edge case. Is this behavior okay or should I file an issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is fine.

@codecov
Copy link

codecov bot commented Apr 15, 2021

Codecov Report

Merging #4741 (ec7566c) into main (090b877) will decrease coverage by 0.00%.
The diff coverage is 89.28%.

@@            Coverage Diff             @@
##             main    #4741      +/-   ##
==========================================
- Coverage   95.52%   95.52%   -0.01%     
==========================================
  Files        1196     1199       +3     
  Lines      274480   274432      -48     
  Branches    16594    16595       +1     
==========================================
- Hits       262191   262143      -48     
- Misses      10102    10111       +9     
+ Partials     2187     2178       -9     

@mavasani mavasani merged commit dc24272 into dotnet:main Apr 15, 2021
@Youssef1313 Youssef1313 deleted the ca1802-fp-const-interpolated branch April 15, 2021 12:46
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.

CA1802 false positive on static readonly interpolated strings in Visual Studio 16.9 Preview 3 targeting net472
2 participants