If you have a custom IEnumerable extension method that removes null elements from a collection and you want to tell ReSharper that it removes null elements, you can use the ContractAnnotation attribute.
Here's an example of how to use the ContractAnnotation attribute to tell ReSharper that a custom IEnumerable extension method removes null elements:
using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using JetBrains.Annotations; public static class EnumerableExtensions { [NotNull, ItemCanBeNull] [ContractAnnotation("null => null; notnull => notnull")] public static IEnumerable<T> RemoveNulls<T>([CanBeNull] this IEnumerable<T> source) { return source?.Where(x => x != null); } } In this example, the ContractAnnotation attribute is applied to the RemoveNulls method. The "null => null; notnull => notnull" expression specifies that if the input parameter is null, the method will return null, and if the input parameter is not null, the method will return a collection that is not null.
The NotNull and ItemCanBeNull attributes are also applied to the method to specify that the method returns a collection that is not null, but may contain null elements.
With this setup, ReSharper will understand that the RemoveNulls method removes null elements from a collection, and will no longer raise warnings about null elements in the returned collection.
"Resharper IEnumerable null check"
// Example Code: var nonNullItems = collection.Where(item => item != null);
"Resharper annotate IEnumerable nullability"
// Example Code: [ItemNotNull] var nonNullItems = collection.Where(item => item != null);
"Resharper code inspection IEnumerable null"
// Example Code: // ReSharper disable PossibleMultipleEnumeration var nonNullItems = collection?.Where(item => item != null).ToList(); // ReSharper restore PossibleMultipleEnumeration
"Resharper nullable reference types IEnumerable"
// Example Code: IEnumerable<string?> nullableStrings = GetNullableStrings();
"Resharper suppress nullability warning IEnumerable"
// Example Code: #nullable disable warnings var processedItems = collection.Where(item => item != null); #nullable restore
"Resharper handle null values LINQ methods"
// Example Code: var nonNullItems = collection.Where(item => item != null).ToList();
"Resharper nullable analysis IEnumerable"
// Example Code: var nonNullItems = collection.Where(item => item != null).ToList()!;
"Resharper suppress null check warning"
// Example Code: // ReSharper disable once ConditionIsAlwaysTrueOrFalse var filteredItems = collection.Where(item => item != null);
"Resharper configure nullability IEnumerable"
// Example Code: // ReSharper disable AssignNullToNotNullAttribute var nonNullItems = collection.Where(item => item != null).ToList(); // ReSharper restore AssignNullToNotNullAttribute
"Resharper nullable enable IEnumerable"
// Example Code: #nullable enable var nonNullItems = collection.Where(item => item != null).ToList(); #nullable restore
masstransit highlight anchor sql-server-2000 frameworks gradle-plugin posix autocad query-by-example android-database