Unreachable code after constant false condition for an Assert.

  I have the following custom assertion:

[Conditional("DEBUG"), DebuggerStepThrough, JetBrains.Annotations.AssertionMethod]
public static void Assert(
      [JetBrains.Annotations.AssertionCondition(JetBrains.Annotations.AssertionConditionType.IS_TRUE)] bool condition,
string msg) {...}

But when I use a constant false expression I get "code is heuristically unreachable":
(Same behvior applies for the standard System.Diagnostics.Debug.Assert(false) ).

Is there any setting, attribute or similar I can use so RS doesn't to flag the code as unreachable?

*Edit: Same problem with "Expression is always true", e.g.

System.Diagnostics.Debug.Assert(_factoryTracker != null);
if (_factoryTracker != null)
{ ... }

1 comment
Comment actions Permalink

A workaround (and probably better design choice) is not to use Assert(false) for failing, but instead use e.g. AssertFail(msg) similar to System.Diagnostics.Debug.Assert.Fail:

[Conditional("DEBUG"), DebuggerStepThrough, JetBrains.Annotations.AssertionMethod]
    public static void AssertFail(string msg) { ... }

Since this doesn't have a condition parameter RS will not flag code after as unreachable. However RS marks code after the built in Assert.Fail as unreachable, so dunno if this workaround maybe is working due to a JetBrains.Annotations bug ? :/


Please sign in to leave a comment.