-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Diagnostics] Diagnose comparisons with '.nan' and suggest using '.isNan' instead #33860
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2bd2d21
[AST] Add 'FloatingPoint' known protocol kind
theblixguy 446a58f
[Sema] Emit a diagnostic for comparisons with '.nan' instead of using…
theblixguy 856440b
[Sema] Update '.nan' comparison diagnostic wording
theblixguy 179b689
[Sema] Explicitly check for either arguments to be '.nan' and tweak a…
theblixguy 4bda19a
[Test] Tweak some comments
theblixguy c38a556
[Diagnostic] Change 'isNan' to 'isNaN'
theblixguy eebbd50
[Sema] Fix a bug where firstArg was checked twice for FloatingPoint c…
theblixguy 3892d9c
[Test] Fix comments in test file
theblixguy 359cffc
[NFC] Add a new 'isStandardComparisonOperator' method to 'Identifier'…
theblixguy 20706a2
[NFC] Reuse argument decl extraction code and switch over to the new …
theblixguy 30a0afa
[NFC] Update conformsToKnownProtocol to accept DeclContext and use it…
theblixguy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// RUN: %target-typecheck-verify-swift | ||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////// | ||
/////// Comparison with '.nan' static property instead of using '.isNaN' instance property /////// | ||
////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
// One side is '.nan' and the other isn't. | ||
// Using '==' or '!=' for comparison should suggest using '.isNaN'. | ||
|
||
let double: Double = 0.0 | ||
_ = double == .nan // expected-warning {{comparison with '.nan' using '==' is always false, use 'double.isNaN' to check if 'double' is not a number}} | ||
_ = double != .nan // expected-warning {{comparison with '.nan' using '!=' is always true, use '!double.isNaN' to check if 'double' is a number}} | ||
_ = 0.0 == .nan // // expected-warning {{comparison with '.nan' using '==' is always false, use '0.0.isNaN' to check if '0.0' is not a number}} | ||
|
||
// One side is '.nan' and the other isn't. Using '>=', '>', '<', '<=' for comparison: | ||
// We can't suggest using '.isNaN' here. | ||
|
||
_ = 0.0 >= .nan // expected-warning {{comparison with '.nan' using '>=' is always false}} | ||
_ = .nan > 1.1 // expected-warning {{comparison with '.nan' using '>' is always false}} | ||
_ = .nan < 2.2 // expected-warning {{comparison with '.nan' using '<' is always false}} | ||
_ = 3.3 <= .nan // expected-warning {{comparison with '.nan' using '<=' is always false}} | ||
|
||
// Both sides are '.nan': | ||
// We can't suggest using '.isNaN' here. | ||
|
||
_ = Double.nan == Double.nan // expected-warning {{'.nan' == '.nan' is always false}} | ||
_ = Double.nan != Double.nan // expected-warning {{'.nan' != '.nan' is always true}} | ||
_ = Double.nan < Double.nan // expected-warning {{'.nan' < '.nan' is always false}} | ||
_ = Double.nan <= Double.nan // expected-warning {{'.nan' <= '.nan' is always false}} | ||
_ = Double.nan > Double.nan // expected-warning {{'.nan' > '.nan' is always false}} | ||
_ = Double.nan >= Double.nan // expected-warning {{'.nan' >= '.nan' is always false}} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.