diff --git a/docs/fsharp/language-reference/compiler-messages/fs0067.md b/docs/fsharp/language-reference/compiler-messages/fs0067.md new file mode 100644 index 0000000000000..90069a502adb9 --- /dev/null +++ b/docs/fsharp/language-reference/compiler-messages/fs0067.md @@ -0,0 +1,32 @@ +--- +description: "Learn more about: FS0067: This type test or downcast will always hold" +title: "Compiler error FS0067" +ms.date: 9/10/2025 +f1_keywords: + - "FS0067" +helpviewer_keywords: + - "FS0067" +--- + +# FS0067: This type test or downcast will always hold + +This message is given when attempting to perform a type test ([:?](../match-expressions.md)) or downcast ([:?>](../casting-and-conversions.md#downcasting)) that will always succeed based on the types involved, making the operation unnecessary. + +Redundant Type test: + +[!code-fsharp[FS0067-redundant-type-test](~/samples/snippets/fsharp/compiler-messages/fs0067.fsx#L2-L8)] + +Redundant Downcast: + +[!code-fsharp[FS0067-redundant-downcast](~/samples/snippets/fsharp/compiler-messages/fs0067.fsx#L11-L18)] + +The two examples above cause the compiler to display the following message: + +```text +FS0067: This type test or downcast will always hold +``` + +The use of `:?` and `:?>` operators is preferable when working with: + +- Base classes when the runtime type might differ. +- Values of type `obj` or interfaces types. diff --git a/docs/fsharp/language-reference/compiler-messages/toc.yml b/docs/fsharp/language-reference/compiler-messages/toc.yml index bd3d78920cd34..9e1a0a553ecd1 100644 --- a/docs/fsharp/language-reference/compiler-messages/toc.yml +++ b/docs/fsharp/language-reference/compiler-messages/toc.yml @@ -19,5 +19,7 @@ href: ./fs0037.md - name: FS0052 - Defensive copy href: ./fs0052.md + - name: FS0067 - This type test or downcast will always hold + href: ./fs0067.md - name: FS0703 - Expected type parameter, not unit-of-measure parameter href: ./fs0703.md diff --git a/samples/snippets/fsharp/compiler-messages/fs0067.fsx b/samples/snippets/fsharp/compiler-messages/fs0067.fsx new file mode 100644 index 0000000000000..6f5eb276bdef3 --- /dev/null +++ b/samples/snippets/fsharp/compiler-messages/fs0067.fsx @@ -0,0 +1,18 @@ +(* Redundant Type test *) +type Dog() = + member this.Bark() = printfn "Woof!" + +let dog = Dog() + +if dog :? Dog then + dog.Bark() + +(* Redundant Downcast *) +type Cat(name: string) = + member this.Name = name + +let cat = Cat("Kitten") + +let sameCat = cat :?> Cat + +printfn "It's still a %s" sameCat.Name