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

New Analyzer: Warn when calling Enumerable.Cast<T>/OfType<T> with incompatible types #4328

Merged
merged 7 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CA1856 | Performance | Error | ConstantExpectedAnalyzer, [Documentation](https:/
CA1857 | Performance | Warning | ConstantExpectedAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1857)
CA1858 | Performance | Info | UseStartsWithInsteadOfIndexOfComparisonWithZero, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1858)
CA1859 | Performance | Info | UseConcreteTypeAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1859)
CA2021 | Reliability | Warning | DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesAnalyzer, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2021)

### Removed Rules

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,12 @@
<data name="DoNotAddArchiveItemPathToTheTargetFileSystemPathMessage" xml:space="preserve">
<value>When creating path for '{0} in method {1}' from relative archive item path to extract file and the source is an untrusted zip archive, make sure to sanitize relative archive item path '{2} in method {3}'</value>
</data>
<data name="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesTitle" xml:space="preserve">
<value>Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</value>
</data>
<data name="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageOfType" xml:space="preserve">
<value>This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</value>
</data>
<data name="DoNotCreateTasksWithoutPassingATaskSchedulerTitle" xml:space="preserve">
<value>Do not create tasks without passing a TaskScheduler</value>
</data>
Expand Down Expand Up @@ -1722,6 +1728,15 @@
<data name="ModuleInitializerAttributeShouldNotBeUsedInLibrariesTitle" xml:space="preserve">
<value>The 'ModuleInitializer' attribute should not be used in libraries</value>
</data>
<data name="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesDescription" xml:space="preserve">
<value>Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</value>
</data>
<data name="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageCast" xml:space="preserve">
<value>Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</value>
</data>
<data name="UseAsyncMethodInAsyncContextMessage" xml:space="preserve">
<value>'{0}' synchronously blocks. Await '{1}' instead.</value>
</data>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,32 @@
<target state="translated">Při deserializaci instance třídy {0} může metoda {1} volat nebezpečnou metodu {2}. Možná volání metod: {3}</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesDescription">
<source>Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</source>
<target state="new">Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageCast">
<source>Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</source>
<target state="new">Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageOfType">
<source>This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</source>
<target state="new">This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesTitle">
<source>Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</source>
<target state="new">Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallToImmutableCollectionOnAnImmutableCollectionValueMessage">
<source>Do not call {0} on an {1} value</source>
<target state="translated">Nevolejte {0} pro hodnotu {1}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,32 @@
<target state="translated">Wenn eine Instanz der Klasse "{0}" deserialisiert wird, kann die Methode "{1}" die gefährliche Methode "{2}" aufrufen. Mögliche Methodenaufrufe: {3}</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesDescription">
<source>Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</source>
<target state="new">Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageCast">
<source>Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</source>
<target state="new">Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageOfType">
<source>This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</source>
<target state="new">This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesTitle">
<source>Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</source>
<target state="new">Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallToImmutableCollectionOnAnImmutableCollectionValueMessage">
<source>Do not call {0} on an {1} value</source>
<target state="translated">"{0}" nicht für einen {1}-Wert aufrufen</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,32 @@
<target state="translated">Al deserializar una instancia de la clase {0}, el método {1} puede llamar al método peligroso {2}. Invocaciones de métodos posibles: {3}.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesDescription">
<source>Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</source>
<target state="new">Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageCast">
<source>Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</source>
<target state="new">Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageOfType">
<source>This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</source>
<target state="new">This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesTitle">
<source>Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</source>
<target state="new">Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallToImmutableCollectionOnAnImmutableCollectionValueMessage">
<source>Do not call {0} on an {1} value</source>
<target state="translated">No llame a {0} en un valor {1}</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,32 @@
<target state="translated">Quand vous désérialisez une instance de la classe {0}, la méthode {1} peut appeler une méthode dangereuse {2}. Les appels de méthode potentiels sont : {3}.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesDescription">
<source>Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</source>
<target state="new">Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageCast">
<source>Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</source>
<target state="new">Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageOfType">
<source>This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</source>
<target state="new">This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesTitle">
<source>Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</source>
<target state="new">Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallToImmutableCollectionOnAnImmutableCollectionValueMessage">
<source>Do not call {0} on an {1} value</source>
<target state="translated">Ne pas appeler {0} sur une valeur {1}</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,32 @@
<target state="translated">Durante la deserializzazione di un'istanza della classe {0} il metodo {1} può chiamare il metodo pericoloso {2}. Le potenziali chiamate al metodo sono: {3}.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesDescription">
<source>Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</source>
<target state="new">Enumerable.Cast&lt;T&gt; and Enumerable.OfType&lt;T&gt; require compatible types to function expectedly.
The generic cast (IL 'unbox.any') used by the sequence returned by Enumerable.Cast&lt;T&gt; will throw InvalidCastException at runtime on elements of the types specified.
The generic type check (C# 'is' operator/IL 'isinst') used by Enumerable.OfType&lt;T&gt; will never succeed with elements of types specified, resulting in an empty sequence.
Widening and user defined conversions are not supported with generic types.</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageCast">
<source>Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</source>
<target state="new">Type '{0}' is incompatible with type '{1}' and cast attempts will throw InvalidCastException at runtime</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesMessageOfType">
<source>This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</source>
<target state="new">This call will always result in an empty sequence because type '{0}' is incompatible with type '{1}'</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesTitle">
<source>Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</source>
<target state="new">Do not call Enumerable.Cast&lt;T&gt; or Enumerable.OfType&lt;T&gt; with incompatible types</target>
<note />
</trans-unit>
<trans-unit id="DoNotCallToImmutableCollectionOnAnImmutableCollectionValueMessage">
<source>Do not call {0} on an {1} value</source>
<target state="translated">Non chiamare {0} su un valore {1}</target>
Expand Down
Loading