Examples of errors detected by the V3021 diagnostic.


V3021. There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless.


MonoDevelop

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless ICSharpCode.NRefactory.CSharp.Refactoring ParameterCanBeDeclaredWithBaseTypeIssue.cs 356


public override void VisitIndexerExpression(
                      IndexerExpression indexerExpression)
{
  ....
  var localResolveResult = context.Resolve(
                             indexerExpression.Target)
                           as LocalResolveResult;
  if (localResolveResult == null)
    return;
  var resolveResult = context.Resolve(indexerExpression);
  if (localResolveResult == null)
    return;
  ....
}

MonoDevelop

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless ICSharpCode.NRefactory.CSharp CombineQueryExpressions.cs 114


bool TryRemoveTransparentIdentifier(....)
{
  ....
  string nae1Name = ExtractExpressionName(ref nae1);
  if (nae1Name == null)
    return false;

  ....
  string nae2Name = ExtractExpressionName(ref nae2);
  if (nae1Name == null)
    return false;

  ....
}

MonoDevelop

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless Xwt.WPF DataConverter.cs 217


public static SW.FontWeight
  ToWpfFontWeight (this FontWeight value)
{
  if (value == FontWeight.Thin)
    return SW.FontWeights.Thin;
  if (value == FontWeight.Ultralight)
    return SW.FontWeights.UltraLight;
  if (value == FontWeight.Light)
    return SW.FontWeights.Light;
  if (value == FontWeight.Semilight)
    return SW.FontWeights.Light;
  if (value == FontWeight.Book)  // <=
    return SW.FontWeights.Normal;
  if (value == FontWeight.Medium)
    return SW.FontWeights.Medium;
  if (value == FontWeight.Semibold)
    return SW.FontWeights.SemiBold;
  if (value == FontWeight.Bold)
    return SW.FontWeights.Bold;
  if (value == FontWeight.Ultrabold)
    return SW.FontWeights.UltraBold;
  if (value == FontWeight.Heavy)
    return SW.FontWeights.Black;
  if (value == FontWeight.Ultraheavy)
    return SW.FontWeights.UltraBlack;

  return SW.FontWeights.Normal;
}

public enum FontWeight
{
  /// The thin weight (100)
  Thin = 100,
  /// The ultra light weight (200)
  Ultralight = 200,
  /// The light weight (300)
  Light = 300,
  /// The semi light weight (350)
  Semilight = 350,
  /// The book weight (380)
  Book = 350,
  ....
}

MonoDevelop

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless Xwt.Gtk ClipboardBackend.cs 86


public override object GetData (TransferDataType type)
{
  if (type == TransferDataType.Text)
    return clipboard.WaitForText ();
  if (type == TransferDataType.Text)
    return clipboard.WaitForImage ();
  ....
}

IronPython and IronRuby

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless. cPickle.cs 2194


private void LoadNewObj(CodeContext/*!*/ context) {
  PythonTuple args = PopStack() as PythonTuple;
  if (args == null) {
    throw PythonOps.TypeError(
      "expected second argument, got {0}",
      DynamicHelpers.GetPythonType(args));
  }
  PythonType cls = PopStack() as PythonType;
  if (args == null) {
    throw PythonOps.TypeError(
      "expected first argument, got {0}",
      DynamicHelpers.GetPythonType(args));
  }
  ....
}

IronPython and IronRuby

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless. SourceLocation.cs 156


public static int Compare(SourceLocation left,
                          SourceLocation right) {
  if (left < right) return -1;
  if (right > left) return 1;
  return 0;
}

.NET Compiler Platform ("Roslyn")

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless InMethodBinder.cs 264


internal static bool ReportConflictWithParameter(....)
{
  ....
  if (newSymbolKind == SymbolKind.Parameter ||
      newSymbolKind == SymbolKind.Local)
  {
    diagnostics.Add(ErrorCode.ERR_LocalSameNameAsTypeParam,
                    newLocation, name);
    return true;
  }
  if (newSymbolKind == SymbolKind.TypeParameter)
  {
    return false;
  }
  if (newSymbolKind == SymbolKind.Parameter ||
      newSymbolKind == SymbolKind.Local)
  {
    diagnostics.Add(ErrorCode.ERR_LocalSameNameAsTypeParam,
                    newLocation, name);
    return true;
  }
  ....
}

Similar errors can be found in some other places:

  • V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless WithLambdaParametersBinder.cs 131

Accord.Net

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless Accord.Statistics MultivariateEmpiricalDistribution.cs 653


public void Fit(double[][] observations,
                double[] weights,
                MultivariateEmpiricalOptions options)
{
  if (weights != null)
    throw new ArgumentException(
      "This distribution does not support weighted  samples.",
      "weights");
  ....
  if (weights != null)
      weights = inPlace ? weights : (double[])weights.Clone();
  ....
}

Similar errors can be found in some other places:

  • V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless Accord.Statistics MultivariateEmpiricalDistribution.cs 687

Old NASA World Wind (C#)

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless HtmlEditor.cs 1480


public void LoadUrl(String url)
{
  ....
  if (!isCreated)
  {
    Debug.WriteLine("Doc not created" +
                    iLoadAttempts.ToString());
    if (iLoadAttempts < 2)
    {
      this.bLoadUrlWhenReady = true;
      return;
    }
    else
    {
      throw new HtmlEditorException("Document not created");
    }
  }

  if (!isCreated) return; // <=
  ....
}

Unity3D

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless UnityEngine.UI StencilMaterial.cs 64


public static Material Add(....)
{
  ....
  if (!baseMat.HasProperty("_StencilReadMask"))
  {
    Debug.LogWarning(".... _StencilReadMask ....", baseMat);
    return baseMat;
  }
  if (!baseMat.HasProperty("_StencilReadMask")) // <=
  {
    Debug.LogWarning(".... _StencilWriteMask ....", baseMat);
    return baseMat;
  }
  ....
}

Mono

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless corlib-net_4_x String.cs 287


public int LastIndexOfAny (char [] anyOf,
                           int startIndex,
                           int count)
{
  ....
  if (this.m_stringLength == 0)
    return -1;

  ....
  if (this.m_stringLength == 0)
    return -1;

  ....
}

Mono

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless System.Drawing-net_4_x PrintingServicesUnix.cs 744


private PaperKind GetPaperKind (int width, int height)
{
  ....
  if (width == 1100 && height == 1700)
    return PaperKind.Standard11x17;
  ....
  if (width == 1100 && height == 1700)
    return PaperKind.Tabloid;
  ....
}

Similar errors can be found in some other places:

  • V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless. Mono.Data.Sqlite-net_4_x SQLiteDataReader.cs 270
  • V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless. System.Web-net_4_x HttpUtility.cs 220
  • V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless., System.Design-plaindesign-net_4_x CodeDomComponentSerializationService.cs 562
  • And 1 additional diagnostic messages.

Mono

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless System.Design-plaindesign-net_4_x CodeDomComponentSerializationService.cs 562


private void SerializeCore (SerializationStore store,
                            object value, bool absolute)
{
  if (value == null)
    throw new ArgumentNullException ("value");
  if (store == null)                            // <=
    throw new ArgumentNullException ("store");

  CodeDomSerializationStore codeDomStore =
    store as CodeDomSerializationStore;
  if (store == null)                            // <=
    throw new InvalidOperationException (
                "store type unsupported");

  codeDomStore.AddObject (value, absolute);
}

SharpDevelop

V3021 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless NamespaceTreeNode.cs 87


public int Compare(SharpTreeNode x, SharpTreeNode y)
{
  ....
  if (typeNameComparison == 0) {
    if (x.Text.ToString().Length < y.Text.ToString().Length)
      return -1;
    if (x.Text.ToString().Length < y.Text.ToString().Length)
      return 1;
  }
  ....
}


Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++ and C#

goto PVS-Studio;