Warn for is-pattern using a relational pattern with a known result.#42501
Conversation
…into patterns3-is-always-true-false
…into patterns3-is-always-true-false
47db30f to
7d4d669
Compare
|
@agocke @RikkiGibson Can you please review this small pattern-matching change? |
| else if (values?.Complement().IsEmpty != true) | ||
| { | ||
| tests.Add(new Tests.One(new BoundDagRelationalTest(rel.Syntax, rel.Relation, rel.ConstantValue, output, rel.HasErrors))); | ||
| } |
There was a problem hiding this comment.
Consider wrapping block in if (fac is { }) { ... } to avoid multiple ?. operators and == true. #ByDesign
There was a problem hiding this comment.
I tried that, but ended up having to repeat line 625 in multiple else clauses.
In reply to: 395854215 [](ancestors = 395854215)
| case BoundDagRelationalTest d: | ||
| var f = ValueSetFactory.ForSpecialType(input.Type.SpecialType); | ||
| if (f is null) return null; | ||
| // TODO: When ValueSetFactory has a method for comparing two values, use it. |
There was a problem hiding this comment.
TODO [](start = 31, length = 4)
PROTOTYPE? #ByDesign
There was a problem hiding this comment.
No. The code works correctly as is. This is a suggestion for a future optimization, which I expect to happen after merging into master.
In reply to: 395854439 [](ancestors = 395854439)
There was a problem hiding this comment.
Should this be also filed as a follow-up issue? #Resolved
There was a problem hiding this comment.
That wouldn't be particularly helpful to me, though I don't object to it.
In reply to: 397252372 [](ancestors = 397252372)
| } | ||
| "; | ||
| CreateCompilation(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.RegularWithPatternCombinators).VerifyDiagnostics( | ||
| // (7,18): error CS8120: The switch case has already been handled by a previous case. |
There was a problem hiding this comment.
error CS8120: The switch case has already been handled by a previous case. [](start = 31, length = 74)
This diagnostic message is confusing since there is no previous case. #Resolved
There was a problem hiding this comment.
Not sure what to do about this, as this is the message used (since C# 7.0) when a switch case is unreachable. What do you suggest? Give a different message if it is the first case? Reword it to be technically correct "Any value that would match this case has been handled by a previous case." ?
In reply to: 395883716 [](ancestors = 395883716)
There was a problem hiding this comment.
Or perhaps "Unreachable case. If there are values that satisfy this case, those values have been handled in previous cases."
In reply to: 395908208 [](ancestors = 395908208,395883716)
| // (7,18): error CS8120: The switch case has already been handled by a previous case. | ||
| // case < int.MinValue: break; // 1 | ||
| Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "< int.MinValue").WithLocation(7, 18), | ||
| // (9,18): error CS8120: The switch case has already been handled by a previous case. |
There was a problem hiding this comment.
error CS8120: The switch case has already been handled by a previous case. [](start = 31, length = 74)
This message is confusing since the previous cases do not handle this (out of range) case. #Resolved
… impossible to match.
|
@cston Do these error messages look better? |
…into patterns3-is-always-true-false
…arning per design review.
|
@cston @jcouv @agocke @RikkiGibson Would you please have a look at the last iteration of this PR? It now gives warnings for more scenarios of always-true is-pattern expressions as requested at the feature design review yesterday. |
| // (6,29): error CS8521: Pattern-matching is not permitted for pointer types. | ||
| // bool M1(int* p) => p is null; // 1 | ||
| Diagnostic(ErrorCode.ERR_PointerTypeInPatternMatching, "null").WithLocation(6, 29), | ||
| // (7,24): warning CS8794: An expression of type 'int*' always matches the provided pattern. |
There was a problem hiding this comment.
nit: should/could we try and avoid the cascade here? The error (not pattern-matching on pointer type) seems more fundamental
| c.VerifyTypes(); | ||
| c.VerifyDiagnostics( | ||
| // (6,13): warning CS8794: An expression of type 'object' always matches the provided pattern. | ||
| // if (x is var _) |
There was a problem hiding this comment.
This should be added to the breaking changes document and thread.
jcouv
left a comment
There was a problem hiding this comment.
LGTM Thanks (iteration 8), modulo documentation of breaking change (such as warning on var _)
|
@jcouv I removed the breaking change. |
Relates to #40727 (patterns for C# 9)