Skip to content

Redundant Assertions

Michael Damatov edited this page Jan 17, 2024 · 7 revisions

If known at some point that x is not null, highlight the assertion as redundant:

  • Debug.Assert(x != null)
  • Contract.Assert(x != null)
  • .AssertNotNull(x)*

💡 A context action is always available to remove the null-forgiving operator or the assertion.

* The inline assertion is determined when all the conditions are met:

  • the method name starts with "AssertNotNull"
  • the method is an extension method
  • the method has single parameter
  • the method's parameter type equals to the method return type
  • the method is annotated with the [NotNull]
  • the method is annotated with the [DebuggerStepThrough]

💡 The analyzer can be deactivated in the ReSharper Options dialog.