Skip to content
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

Moving WarnExpressionTests over to NUnit #7232

Merged
merged 2 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 208 additions & 0 deletions tests/fsharp/Compiler/ErrorMessages/WarnExpressionTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace FSharp.Compiler.UnitTests

open NUnit.Framework
open FSharp.Compiler.SourceCodeServices

[<TestFixture>]
module ``Warn Expression`` =

[<Test>]
let ``Warn If Expression Result Unused``() =
CompilerAssert.TypeCheckSingleError
"""
1 + 2
printfn "%d" 3
"""
FSharpErrorSeverity.Warning
20
(2, 1, 2, 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'."

[<Test>]
let ``Warn If Possible Assignment``() =
CompilerAssert.TypeCheckSingleError
"""
let x = 10
let y = "hello"

let changeX() =
x = 20
y = "test"
"""
FSharpErrorSeverity.Warning
20
(6, 5, 6, 11)
"The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to mutate a value, then mark the value 'mutable' and use the '<-' operator e.g. 'x <- expression'."

[<Test>]
let ``Warn If Possible Assignment To Mutable``() =
CompilerAssert.TypeCheckSingleError
"""
let mutable x = 10
let y = "hello"

let changeX() =
x = 20
y = "test"
"""
FSharpErrorSeverity.Warning
20
(6, 5, 6, 11)
"The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to mutate a value, then use the '<-' operator e.g. 'x <- expression'."

[<Test>]
let ``Warn If Possible dotnet Property Setter``() =
CompilerAssert.TypeCheckWithErrors
"""
open System

let z = System.Timers.Timer()
let y = "hello"

let changeProperty() =
z.Enabled = true
y = "test"
"""
[|
FSharpErrorSeverity.Warning, 760, (4, 9, 4, 30), "It is recommended that objects supporting the IDisposable interface are created using the syntax 'new Type(args)', rather than 'Type(args)' or 'Type' as a function value representing the constructor, to indicate that resources may be owned by the generated value"
FSharpErrorSeverity.Warning, 20, (8, 5, 8, 21), "The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to set a value to a property, then use the '<-' operator e.g. 'z.Enabled <- expression'."
|]

[<Test>]
let ``Don't Warn If Property Without Setter``() =
CompilerAssert.TypeCheckSingleError
"""
type MyClass(property1 : int) =
member val Property2 = "" with get

let x = MyClass(1)
let y = "hello"

let changeProperty() =
x.Property2 = "22"
y = "test"
"""
FSharpErrorSeverity.Warning
20
(9, 5, 9, 23)
"The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'."

[<Test>]
let ``Warn If Implicitly Discarded``() =
CompilerAssert.TypeCheckSingleError
"""
let x = 10
let y = 20

let changeX() =
y * x = 20
y = 30
"""
FSharpErrorSeverity.Warning
20
(6, 5, 6, 15)
"The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'."

[<Test>]
let ``Warn If Discarded In List``() =
CompilerAssert.TypeCheckSingleError
"""
let div _ _ = 1
let subView _ _ = [1; 2]

// elmish view
let view model dispatch =
[
yield! subView model dispatch
div [] []
]
"""
FSharpErrorSeverity.Warning
3221
(9, 8, 9, 17)
"This expression returns a value of type 'int' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield'."

[<Test>]
let ``Warn If Discarded In List 2``() =
CompilerAssert.TypeCheckSingleError
"""
// stupid things to make the sample compile
let div _ _ = 1
let subView _ _ = [1; 2]
let y = 1

// elmish view
let view model dispatch =
[
div [] [
match y with
| 1 -> yield! subView model dispatch
| _ -> subView model dispatch
]
]
"""
FSharpErrorSeverity.Warning
3222
(13, 19, 13, 41)
"This expression returns a value of type 'int list' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield!'."

[<Test>]
let ``Warn If Discarded In List 3``() =
CompilerAssert.TypeCheckSingleError
"""
// stupid things to make the sample compile
let div _ _ = 1
let subView _ _ = true
let y = 1

// elmish view
let view model dispatch =
[
div [] [
match y with
| 1 -> ()
| _ -> subView model dispatch
]
]
"""
FSharpErrorSeverity.Warning
20
(13, 19, 13, 41)
"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'."

[<Test>]
let ``Warn Only On Last Expression``() =
CompilerAssert.TypeCheckSingleError
"""
let mutable x = 0
while x < 1 do
printfn "unneeded"
x <- x + 1
true
"""
FSharpErrorSeverity.Warning
20
(6, 5, 6, 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'."

[<Test>]
let ``Warn If Possible Property Setter``() =
CompilerAssert.TypeCheckSingleError
"""
type MyClass(property1 : int) =
member val Property1 = property1
member val Property2 = "" with get, set

let x = MyClass(1)
let y = "hello"

let changeProperty() =
x.Property2 = "20"
y = "test"
"""
FSharpErrorSeverity.Warning
20
(10, 5, 10, 23)
"The result of this equality expression has type 'bool' and is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to set a value to a property, then use the '<-' operator e.g. 'x.Property2 <- expression'."
1 change: 1 addition & 0 deletions tests/fsharp/FSharpSuite.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Compile Include="Compiler\ErrorMessages\AccessOfTypeAbbreviationTests.fs" />
<Compile Include="Compiler\ErrorMessages\ElseBranchHasWrongTypeTests.fs" />
<Compile Include="Compiler\ErrorMessages\MissingElseBranch.fs" />
<Compile Include="Compiler\ErrorMessages\WarnExpressionTests.fs" />
<Compile Include="Compiler\SourceTextTests.fs" />
<Compile Include="Compiler\Language\AnonRecordTests.fs" />
<Compile Include="Compiler\Language\SpanOptimizationTests.fs" />
Expand Down
14 changes: 0 additions & 14 deletions tests/fsharpqa/Source/Warnings/DontWarnIfPropertyWithoutSetter.fs

This file was deleted.

14 changes: 0 additions & 14 deletions tests/fsharpqa/Source/Warnings/WarnIfDiscardedInList.fs

This file was deleted.

19 changes: 0 additions & 19 deletions tests/fsharpqa/Source/Warnings/WarnIfDiscardedInList2.fs

This file was deleted.

19 changes: 0 additions & 19 deletions tests/fsharpqa/Source/Warnings/WarnIfDiscardedInList3.fs

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions tests/fsharpqa/Source/Warnings/WarnIfImplicitlyDiscarded.fs

This file was deleted.

11 changes: 0 additions & 11 deletions tests/fsharpqa/Source/Warnings/WarnIfPossibleAssignment.fs

This file was deleted.

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions tests/fsharpqa/Source/Warnings/WarnIfPossiblePropertySetter.fs

This file was deleted.

10 changes: 0 additions & 10 deletions tests/fsharpqa/Source/Warnings/WarnOnlyOnLastExpression.fs

This file was deleted.

11 changes: 0 additions & 11 deletions tests/fsharpqa/Source/Warnings/env.lst
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,8 @@
SOURCE=DowncastInsteadOfUpcast.fs # DowncastInsteadOfUpcast.fs
SOURCE=RuntimeTypeTestInPattern.fs # RuntimeTypeTestInPattern.fs
SOURCE=RuntimeTypeTestInPattern2.fs # RuntimeTypeTestInPattern2.fs
SOURCE=WarnIfExpressionResultUnused.fs # WarnIfExpressionResultUnused.fs
SOURCE=MemberHasMultiplePossibleDispatchSlots.fs # MemberHasMultiplePossibleDispatchSlots.fs
SOURCE=Repro1548.fs SCFLAGS="-r:Repro1548.dll" # Repro1548.fs
SOURCE=WarnIfPossibleAssignment.fs
SOURCE=WarnIfPossibleAssignmentToMutable.fs
SOURCE=WarnIfPossibleDotNetPropertySetter.fs
SOURCE=DontWarnIfPropertyWithoutSetter.fs
SOURCE=WarnIfImplicitlyDiscarded.fs
SOURCE=WarnIfDiscardedInList.fs
SOURCE=WarnIfDiscardedInList2.fs
SOURCE=WarnIfDiscardedInList3.fs
SOURCE=WarnOnlyOnLastExpression.fs
SOURCE=WarnIfPossiblePropertySetter.fs
SOURCE=DoCannotHaveVisibilityDeclarations.fs
SOURCE=ModuleAbbreviationsArePrivate.fs
SOURCE=DontSuggestIntentionallyUnusedVariables.fs SCFLAGS="--vserrors" # DontSuggestIntentionallyUnusedVariables.fs