diff --git a/docs/release-notes/.FSharp.Compiler.Service/11.0.100.md b/docs/release-notes/.FSharp.Compiler.Service/11.0.100.md index 9509ac72a3a..c9ff307f2d2 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/11.0.100.md +++ b/docs/release-notes/.FSharp.Compiler.Service/11.0.100.md @@ -16,8 +16,9 @@ * Fix DU case names matching IWSAM member names no longer cause duplicate property entries. (Issue [#14321](https://github.com/dotnet/fsharp/issues/14321), [PR #19341](https://github.com/dotnet/fsharp/pull/19341)) * Fix DefaultAugmentation(false) duplicate entry in method table. (Issue [#16565](https://github.com/dotnet/fsharp/issues/16565), [PR #19341](https://github.com/dotnet/fsharp/pull/19341)) * Fix abstract event accessors now have SpecialName flag. (Issue [#5834](https://github.com/dotnet/fsharp/issues/5834), [PR #19341](https://github.com/dotnet/fsharp/pull/19341)) -* Fix warning 20 ("expression is implicitly ignored") pointing at the wrong range when the last expression in a sequential block (e.g. inside `for`, `while` loops) is non-unit. The squiggle now correctly highlights only the offending expression. ([Issue #5735](https://github.com/dotnet/fsharp/issues/5735), [PR #19504](https://github.com/dotnet/fsharp/pull/19504)) +* Fix warning 20 ("expression is implicitly ignored") pointing at the wrong range when the last expression in a sequential block (e.g. inside `for`, `while` loops) is non-unit. The squiggle now correctly highlights only the offending expression. ([Issue #5418](https://github.com/dotnet/fsharp/issues/5418), [PR #19504](https://github.com/dotnet/fsharp/pull/19504)) * Fix missing "No implementation was given" error when F# class inherits from a C# class with `abstract override` members without providing an implementation. ([Issue #7776](https://github.com/dotnet/fsharp/issues/7776), [PR #19503](https://github.com/dotnet/fsharp/pull/19503)) +* Extend the warning 20 range fix to also walk through `let`/`use` bindings, so the squiggle lands on the offending expression when it is the body of a `let`/`use`. ([Issue #5418](https://github.com/dotnet/fsharp/issues/5418)) * Fix CLIEvent properties to be correctly recognized as events: `IsEvent` returns `true` and `XmlDocSig` uses `E:` prefix instead of `P:`. ([Issue #10273](https://github.com/dotnet/fsharp/issues/10273), [PR #18584](https://github.com/dotnet/fsharp/pull/18584)) * Fix extra sequence point at the end of match expressions. ([Issue #12052](https://github.com/dotnet/fsharp/issues/12052), [PR #19278](https://github.com/dotnet/fsharp/pull/19278)) * Fix wrong sequence point range for `return`/`yield`/`return!`/`yield!` inside computation expressions. ([Issue #19248](https://github.com/dotnet/fsharp/issues/19248), [PR #19278](https://github.com/dotnet/fsharp/pull/19278)) diff --git a/src/Compiler/Checking/Expressions/CheckExpressions.fs b/src/Compiler/Checking/Expressions/CheckExpressions.fs index f6e0a61663f..e6d92917012 100644 --- a/src/Compiler/Checking/Expressions/CheckExpressions.fs +++ b/src/Compiler/Checking/Expressions/CheckExpressions.fs @@ -5582,12 +5582,14 @@ and TcStmt (cenv: cenv) env tpenv synExpr = let g = cenv.g let expr, ty, tpenv = TcExprOfUnknownType cenv env tpenv synExpr - // Use the range of the last expression in a sequential chain for warnings, - // so that "expression is ignored" diagnostics point at the offending expression - // rather than the entire sequential body. See https://github.com/dotnet/fsharp/issues/5735 + // Use the range of the last expression in a sequential chain (also walking through + // 'let'/'use' bindings) for warnings, so that "expression is ignored" diagnostics point + // at the offending expression rather than the entire sequential body. + // See https://github.com/dotnet/fsharp/issues/5418 let rec lastExprRange (e: SynExpr) = match e with | SynExpr.Sequential(expr2 = expr2) -> lastExprRange expr2 + | SynExpr.LetOrUse({ Body = body }) -> lastExprRange body | _ -> e.Range let m = lastExprRange synExpr diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs index bc68eb4d7d9..6a8e6f789d1 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs @@ -176,7 +176,7 @@ while x < 1 do |> withSingleDiagnostic (Warning 20, Line 6, Col 5, Line 6, Col 9, "The result of this expression has type 'bool' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") - // https://github.com/dotnet/fsharp/issues/5735 + // https://github.com/dotnet/fsharp/issues/5418 [] let ``Warn On Last Expression In For Loop - int``() = FSharp """ @@ -192,7 +192,7 @@ for i in 1 .. 10 do |> withSingleDiagnostic (Warning 20, Line 7, Col 5, Line 7, Col 8, "The result of this expression has type 'int' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") - // https://github.com/dotnet/fsharp/issues/5735 + // https://github.com/dotnet/fsharp/issues/5418 [] let ``Warn On Last Expression In For Loop - string``() = FSharp """ @@ -205,7 +205,7 @@ for i in 1 .. 10 do |> withSingleDiagnostic (Warning 20, Line 4, Col 5, Line 4, Col 12, "The result of this expression has type 'string' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") - // https://github.com/dotnet/fsharp/issues/5735 + // https://github.com/dotnet/fsharp/issues/5418 [] let ``Warn On Last Expression In Integer For Loop``() = FSharp """ @@ -218,7 +218,7 @@ for i = 1 to 10 do |> withSingleDiagnostic (Warning 20, Line 4, Col 5, Line 4, Col 7, "The result of this expression has type 'int' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") - // https://github.com/dotnet/fsharp/issues/5735 + // https://github.com/dotnet/fsharp/issues/5418 [] let ``Warn On Last Expression In While Loop - non-bool``() = FSharp """ @@ -233,6 +233,33 @@ while x < 1 do |> withSingleDiagnostic (Warning 20, Line 6, Col 5, Line 6, Col 8, "The result of this expression has type 'int' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") + // https://github.com/dotnet/fsharp/issues/5418 + [] + let ``Warn On Last Expression In For Loop - non-unit after let binding``() = + FSharp """ +for _ in [] do + let x = 1 + x + """ + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Warning 20, Line 4, Col 5, Line 4, Col 6, + "The result of this expression has type 'int' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") + + // https://github.com/dotnet/fsharp/issues/5418 + [] + let ``Warn On Last Expression In For Loop - non-unit after nested let bindings``() = + FSharp """ +for _ in 1 .. 3 do + let a = 1 + let b = 2 + a + b + """ + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Warning 20, Line 5, Col 5, Line 5, Col 10, + "The result of this expression has type 'int' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") + [] let ``Warn If Possible Property Setter``() = FSharp """