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

SE - Nullable: Add Null constraint for empty nullable constructor #6853

Merged
merged 6 commits into from
Mar 6, 2023

Conversation

pavel-mikula-sonarsource
Copy link
Contributor

Part of #6812

new Nullable<int>() should have Null constraint

@@ -67,10 +67,15 @@ public static IEnumerable<SymbolicContext> Process(SymbolicContext context)
{
if (Simple.TryGetValue(context.Operation.Instance.Kind, out var simple)) // Operations that return single state
{
context = context.WithState(simple.Process(context));
return new[] { context.WithState(simple.Process(context)) };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated improvement. If an operation is registered in the 1st list, there's no reason look in the 2nd one too

Choose a reason for hiding this comment

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

Do we have some kind of test or check, so that we don't end up with one operation kind in both lists?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, we don't. It should be easy to catch, as the change goes for "simple" first, so whatever complex logic we'll have should just not work. And should be easy to catch

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM. I have one question regarding the optimization, though.

@@ -67,10 +67,15 @@ public static IEnumerable<SymbolicContext> Process(SymbolicContext context)
{
if (Simple.TryGetValue(context.Operation.Instance.Kind, out var simple)) // Operations that return single state
{
context = context.WithState(simple.Process(context));
return new[] { context.WithState(simple.Process(context)) };

Choose a reason for hiding this comment

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

Do we have some kind of test or check, so that we don't end up with one operation kind in both lists?

{
const string code = """
int? explicitType = new Nullable<int>();
int? targetTyped = new();

Choose a reason for hiding this comment

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

Propably later:

    void M<T>() where T: struct
    {
        T? a = new T(); // NotNull (this should come from me)
        T? b = new T?(); // Null  (this should come from you)
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This already works - so I've added those cases

""";
var validator = SETestContext.CreateCS(code).Validator;
validator.ValidateTag("ExplicitType", x => x.HasConstraint(ObjectConstraint.Null).Should().BeTrue());
validator.ValidateTag("TargetTyped", x => x.HasConstraint(ObjectConstraint.NotNull).Should().BeTrue("new() of int produces value 0"));

Choose a reason for hiding this comment

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

😮 Interesting behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. It took me a debug session and a manual test to verify. definitely deserves a special message :)

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

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.

2 participants