Skip to content

Commit 6a828e2

Browse files
Prefix-to-infix code fix: don't throw if error range extends to EOF (#17448)
1 parent d6507d6 commit 6a828e2

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

docs/release-notes/.VisualStudio/17.12.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
### Fixed
22

3+
* In the prefix-to-infix code fix, don't throw an exception if the error range for FS0003 extends through the end of the source text. ([PR #17448](https://github.com/dotnet/fsharp/pull/17448))
4+
35
### Added
46

57
### Changed

vsintegration/src/FSharp.Editor/CodeFixes/ChangePrefixNegationToInfixSubtraction.fs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ type internal ChangePrefixNegationToInfixSubtractionCodeFixProvider() =
1818
static let title = SR.ChangePrefixNegationToInfixSubtraction()
1919

2020
static let rec findNextNonWhitespacePos (sourceText: SourceText) pos =
21-
if pos < sourceText.Length && Char.IsWhiteSpace sourceText[pos] then
22-
findNextNonWhitespacePos sourceText (pos + 1)
21+
if pos < sourceText.Length - 1 then
22+
if Char.IsWhiteSpace sourceText[pos] then
23+
findNextNonWhitespacePos sourceText (pos + 1)
24+
else
25+
ValueSome pos
2326
else
24-
pos
27+
ValueNone
2528

2629
override _.FixableDiagnosticIds = ImmutableArray.Create "FS0003"
2730

@@ -34,16 +37,15 @@ type internal ChangePrefixNegationToInfixSubtractionCodeFixProvider() =
3437

3538
// in a line like "... x -1 ...",
3639
// squiggly goes for "x", not for "-", hence we search for "-"
37-
let pos = findNextNonWhitespacePos sourceText (context.Span.End + 1)
38-
39-
if sourceText[pos] <> '-' then
40-
return ValueNone
41-
else
42-
return
43-
ValueSome
44-
{
45-
Name = CodeFix.ChangePrefixNegationToInfixSubtraction
46-
Message = title
47-
Changes = [ TextChange(TextSpan(pos + 1, 0), " ") ]
48-
}
40+
let fix =
41+
findNextNonWhitespacePos sourceText (context.Span.End + 1)
42+
|> ValueOption.filter (fun pos -> sourceText[pos] = '-')
43+
|> ValueOption.map (fun pos ->
44+
{
45+
Name = CodeFix.ChangePrefixNegationToInfixSubtraction
46+
Message = title
47+
Changes = [ TextChange(TextSpan(pos + 1, 0), " ") ]
48+
})
49+
50+
return fix
4951
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,23 @@ let x = 1 (+) 2
4848
let actual = codeFix |> tryFix code Auto
4949

5050
Assert.Equal(expected, actual)
51+
52+
[<Fact>]
53+
let ``Doesn't throw when the error range extends to the end of the source text`` () =
54+
let code =
55+
"""
56+
module Microsoft =
57+
module FSharp =
58+
module Core =
59+
module LanguagePrimitives =
60+
module IntrinsicFunctions =
61+
let GetArray2D _ _ = 99
62+
63+
let a = Array2D.init 10 10 (+)
64+
a[5, 5]"""
65+
66+
let expected = None
67+
68+
let actual = codeFix |> tryFix code Auto
69+
70+
Assert.Equal(expected, actual)

0 commit comments

Comments
 (0)