Skip to content

Commit a9003e8

Browse files
More misc parens fixes (#18350)
1 parent e4950e6 commit a9003e8

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

docs/release-notes/.FSharp.Compiler.Service/9.0.300.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Fix nullness warning for overrides of generic code with nullable type instance ([Issue #17988](https://github.com/dotnet/fsharp/issues/17988), [PR #18337](https://github.com/dotnet/fsharp/pull/18337))
1515
* Unsafe downcast from `obj` to generic `T` no longer requires `not null` constraint on `T`([Issue #18275](https://github.com/dotnet/fsharp/issues/18275), [PR #18343](https://github.com/dotnet/fsharp/pull/18343))
1616
* Fix for missing parse diagnostics in TransparentCompiler.ParseAndCheckProject ([PR #18366](https://github.com/dotnet/fsharp/pull/18366))
17+
* Miscellanous parentheses analyzer fixes. ([PR #18350](https://github.com/dotnet/fsharp/pull/18350))
1718

1819
### Added
1920
* Added missing type constraints in FCS. ([PR #18241](https://github.com/dotnet/fsharp/pull/18241))

src/Compiler/Service/SynExpr.fs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ module SynExpr =
343343
| PrefixApp prec -> ValueSome(prec, Non)
344344
| InfixApp(prec, side) -> ValueSome(prec, side)
345345
| SynExpr.App(argExpr = SynExpr.ComputationExpr _) -> ValueSome(UnaryPrefix, Left)
346+
| SynExpr.App(argExpr = SynExpr.Paren(expr = SynExpr.App _ & Is inner)) -> ValueSome(Apply, Right)
346347
| SynExpr.App(funcExpr = SynExpr.Paren(expr = SynExpr.App _)) -> ValueSome(Apply, Left)
347348
| SynExpr.App(flag = ExprAtomicFlag.Atomic) -> ValueSome(Dot, Non)
348349
| SynExpr.App _ -> ValueSome(Apply, Non)
@@ -396,6 +397,7 @@ module SynExpr =
396397
| SynExpr.DotIndexedSet _
397398
| SynExpr.DotNamedIndexedPropertySet _
398399
| SynExpr.DotSet _ -> ValueSome Set
400+
| SynExpr.TypeTest _ -> ValueSome TypeTest
399401
| _ -> ValueNone
400402

401403
module Dangling =
@@ -540,7 +542,7 @@ module SynExpr =
540542

541543
if i >= 0 && i < offsidesCol then
542544
let slice = line.AsSpan(i, min (offsidesCol - i) (line.Length - i))
543-
let j = slice.IndexOfAnyExcept("*/%-+:^@><=!|0$.?".AsSpan())
545+
let j = slice.IndexOfAnyExcept("*/%-+:^@><=!|$.?".AsSpan())
544546

545547
let lo = i + (if j >= 0 && slice[j] = ' ' then j else 0)
546548

@@ -845,22 +847,6 @@ module SynExpr =
845847
// precedence than regular function application.
846848
| _, SyntaxNode.SynExpr(SynExpr.App _) :: SyntaxNode.SynExpr(PrefixApp High) :: _ -> true
847849

848-
// Parens are never required around suffixed or infixed numeric literals, e.g.,
849-
//
850-
// (1l).ToString()
851-
// (1uy).ToString()
852-
// (0b1).ToString()
853-
// (1e10).ToString()
854-
// (1.0).ToString()
855-
| DotSafeNumericLiteral, _ -> false
856-
857-
// Parens are required around bare decimal ints or doubles ending
858-
// in dots when being dotted into, e.g.,
859-
//
860-
// (1).ToString()
861-
// (1.).ToString()
862-
| SynExpr.Const(constant = SynConst.Int32 _ | SynConst.Double _), SyntaxNode.SynExpr(SynExpr.DotGet _) :: _ -> true
863-
864850
// Parens are required around join conditions:
865851
//
866852
// join … on (… = …)
@@ -907,6 +893,22 @@ module SynExpr =
907893
->
908894
true
909895

896+
// Parens are never required around suffixed or infixed numeric literals, e.g.,
897+
//
898+
// (1l).ToString()
899+
// (1uy).ToString()
900+
// (0b1).ToString()
901+
// (1e10).ToString()
902+
// (1.0).ToString()
903+
| DotSafeNumericLiteral, _ -> false
904+
905+
// Parens are required around bare decimal ints or doubles ending
906+
// in dots when being dotted into, e.g.,
907+
//
908+
// (1).ToString()
909+
// (1.).ToString()
910+
| SynExpr.Const(constant = SynConst.Int32 _ | SynConst.Double _), SyntaxNode.SynExpr(SynExpr.DotGet _) :: _ -> true
911+
910912
// The :: operator is parsed differently from other symbolic infix operators,
911913
// so we need to give it special treatment.
912914

@@ -952,6 +954,7 @@ module SynExpr =
952954
let rec loop =
953955
function
954956
| [] -> false
957+
| SynMatchClause(whenExpr = Some(SynExpr.Paren(expr = Dangling.ArrowSensitive _ & Is inner))) :: _ -> true
955958
| SynMatchClause(trivia = trivia) :: clauses ->
956959
trivia.BarRange |> Option.exists (problematic matchOrTryRange)
957960
|| trivia.ArrowRange |> Option.exists (problematic matchOrTryRange)
@@ -1148,6 +1151,7 @@ module SynExpr =
11481151
| _, MulDivMod(Mod, _)
11491152
| _, AddSub(Sub, _) -> true
11501153
| Relational _, Relational _ -> true
1154+
| Apply, Apply -> true
11511155
| _ -> false
11521156

11531157
| c -> c > 0

vsintegration/tests/FSharp.Editor.Tests/CodeFixes/RemoveUnnecessaryParenthesesTests.fs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ let _ =
398398
",
399399
"
400400
match () with
401-
| () when box x :? int
401+
| () when (box x :? int)
402402
-> ()
403403
| _ -> ()
404404
"
@@ -1830,6 +1830,8 @@ in x
18301830
f ((+) x y) z
18311831
"
18321832

1833+
"(Gen.map f << Gen.map g) (Gen.constant x)", "(Gen.map f << Gen.map g) (Gen.constant x)"
1834+
18331835
// TypeApp
18341836
"id (id<int>)", "id id<int>"
18351837

@@ -1890,6 +1892,7 @@ in x
18901892
"""(id "x").Length""", """(id "x").Length"""
18911893
"""(3L.ToString("x")).Length""", """(3L.ToString "x").Length"""
18921894
"~~TypedResults.Ok<string>(maybe.Value)", "~~TypedResults.Ok<string>(maybe.Value)"
1895+
"bg.lighten(0.2).hexa ()", "bg.lighten(0.2).hexa ()"
18931896

18941897
// DotLambda
18951898
"[{| A = x |}] |> List.map (_.A)", "[{| A = x |}] |> List.map _.A"

0 commit comments

Comments
 (0)