diff --git a/docs/release-notes/.VisualStudio/17.12.md b/docs/release-notes/.VisualStudio/17.12.md index c247da5870b..6df973cf607 100644 --- a/docs/release-notes/.VisualStudio/17.12.md +++ b/docs/release-notes/.VisualStudio/17.12.md @@ -1,5 +1,7 @@ ### Fixed +* 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)) + ### Added ### Changed diff --git a/vsintegration/src/FSharp.Editor/CodeFixes/ChangePrefixNegationToInfixSubtraction.fs b/vsintegration/src/FSharp.Editor/CodeFixes/ChangePrefixNegationToInfixSubtraction.fs index 87841d5d57b..ee8b8870538 100644 --- a/vsintegration/src/FSharp.Editor/CodeFixes/ChangePrefixNegationToInfixSubtraction.fs +++ b/vsintegration/src/FSharp.Editor/CodeFixes/ChangePrefixNegationToInfixSubtraction.fs @@ -18,10 +18,13 @@ type internal ChangePrefixNegationToInfixSubtractionCodeFixProvider() = static let title = SR.ChangePrefixNegationToInfixSubtraction() static let rec findNextNonWhitespacePos (sourceText: SourceText) pos = - if pos < sourceText.Length && Char.IsWhiteSpace sourceText[pos] then - findNextNonWhitespacePos sourceText (pos + 1) + if pos < sourceText.Length - 1 then + if Char.IsWhiteSpace sourceText[pos] then + findNextNonWhitespacePos sourceText (pos + 1) + else + ValueSome pos else - pos + ValueNone override _.FixableDiagnosticIds = ImmutableArray.Create "FS0003" @@ -34,16 +37,15 @@ type internal ChangePrefixNegationToInfixSubtractionCodeFixProvider() = // in a line like "... x -1 ...", // squiggly goes for "x", not for "-", hence we search for "-" - let pos = findNextNonWhitespacePos sourceText (context.Span.End + 1) - - if sourceText[pos] <> '-' then - return ValueNone - else - return - ValueSome - { - Name = CodeFix.ChangePrefixNegationToInfixSubtraction - Message = title - Changes = [ TextChange(TextSpan(pos + 1, 0), " ") ] - } + let fix = + findNextNonWhitespacePos sourceText (context.Span.End + 1) + |> ValueOption.filter (fun pos -> sourceText[pos] = '-') + |> ValueOption.map (fun pos -> + { + Name = CodeFix.ChangePrefixNegationToInfixSubtraction + Message = title + Changes = [ TextChange(TextSpan(pos + 1, 0), " ") ] + }) + + return fix } diff --git a/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs b/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs index 5245d8a3ae0..b93055a3a90 100644 --- a/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs +++ b/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs @@ -48,3 +48,23 @@ let x = 1 (+) 2 let actual = codeFix |> tryFix code Auto Assert.Equal(expected, actual) + +[] +let ``Doesn't throw when the error range extends to the end of the source text`` () = + let code = + """ +module Microsoft = + module FSharp = + module Core = + module LanguagePrimitives = + module IntrinsicFunctions = + let GetArray2D _ _ = 99 + +let a = Array2D.init 10 10 (+) +a[5, 5]""" + + let expected = None + + let actual = codeFix |> tryFix code Auto + + Assert.Equal(expected, actual)