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: Learn ObjectConstraint for nullable types #6812

Closed
14 tasks done
pavel-mikula-sonarsource opened this issue Feb 27, 2023 · 2 comments · Fixed by #6961
Closed
14 tasks done

SE: Learn ObjectConstraint for nullable types #6812

pavel-mikula-sonarsource opened this issue Feb 27, 2023 · 2 comments · Fixed by #6961
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues.
Milestone

Comments

@pavel-mikula-sonarsource
Copy link
Contributor

pavel-mikula-sonarsource commented Feb 27, 2023

Add Null constraint to the Roslyn SE for nullable value types.

  • Copy known constraints from nullable value to .Value member
  • Set BoolConstraint on .HasValue from known ObjectConstraint, and do not learn NotNull from property reference
  • Set Null constraint for new Nullable<int>()
  • Set argument constraints for new Nullable<int>(value) or new Nullable<int>(42)
  • Explicit and implicit conversions from/to nullable and non-nullable
  • Set branching ObjectConstraint when branching on .HasValue
  • Set branching ObjectConstraint when branching on null checks
  • Set branching ObjectConstraint when branching on ==value checks
  • Support .GetValueOrDefault() - do not lean "NotNull" on the invocation, propagate constraint
  • Lean from RecursivePattern for nullable types
  • Set ObjectConstraint when on Debug.Assert tests
  • Review SE - Non-nullable value types: Add NonNullableValueTypeCheck #6864 (comment)
  • Take a look at b = x.HasValue & x.Value;
  • Check .Equals(42) and .Equals(null), object.Equals(left, right)

#6840 deals with S2259 overlap

This issue requires more investigation to implement properly.

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: Improvement Area: CFG/SE CFG and SE related issues. Area: C# C# rules related issues. labels Feb 27, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 8.56 milestone Feb 27, 2023
@pavel-mikula-sonarsource
Copy link
Contributor Author

pavel-mikula-sonarsource commented Mar 1, 2023

Having just ObjectConstraint might be enough

int? i = null; // Null?
i.Value    //-> throws
i.GetHashCode()    //-> throws
i.HasValue // -> it's fine
i.GetValueOrDefault() //-> --//--
new Nullable<int>() // -> null constraint
new Nullable<int>(42) // -> not null constraint

We need to look into explicit and implicit conversions between nullable and non-nullable.
Lifting operators, consider #4250

@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title SE: Add NullableConstraint SE: Learn ObjectConstraint for nullable types Mar 1, 2023
@pavel-mikula-sonarsource
Copy link
Contributor Author

Testing if(value.HasValue) needs to learn NotNull or Null constraints if not present.
Testring if(value == something) will be tricky

Debug.Assert(CurrentToken.Kind == SyntaxKind.Keyword && result.HasValue); // Can learn NotNull
            if (_keywordParserMap.TryGetValue(result.Value, out var handler)) // Compliant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants