Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
Examples of errors detected by the V302…

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.


Stride

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. GraphViewModel.cs 40


private GraphViewModel(....) : base(serviceProvider)
{
  ....
  if (rootPresenters == null)
    throw new ArgumentNullException(nameof(rootPresenters));
  ....
  if (rootPresenters == null)
    throw new ArgumentNullException(nameof(rootNode));
  ....
}

Stride

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.ObjectReference.cs 134


internal IObjectNode SetTarget(....)
{
  if (....)
  {
    var targetValue = targetNode.Retrieve();

    if (targetValue != null && !type.IsInstanceOfType(targetValue))
      throw new InvalidOperationException(@"The type of the retrieved" +
                                           "node content does not match" +
                                           "the type of this reference");

    if (targetValue != null && !type.IsInstanceOfType(targetValue))
      throw new InvalidOperationException("TargetNode type does not " +
                                          "match the reference type.");

    ....
  }
  ....
}

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;

  ....
}

ML.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. BoostingFastTree.cs 44, 47


private protected override void CheckOptions(IChannel ch)
{
  ....
  if (FastTreeTrainerOptions.NumberOfLeaves > 2 &&
      FastTreeTrainerOptions.HistogramPoolSize >
      FastTreeTrainerOptions.NumberOfLeaves - 1)
  {
    throw ch.Except("Histogram pool size (ps) must be at least 2.");
  }

  if (FastTreeTrainerOptions.NumberOfLeaves > 2 &&
      FastTreeTrainerOptions.HistogramPoolSize >
      FastTreeTrainerOptions.NumberOfLeaves - 1)
  {
    throw ch.Except("Histogram pool size (ps) must be at most numLeaves - 1.");
  }
  ....
}

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;
  }
  ....
}

Logify Alert Clients

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 Program.cs 159


bool ValidateConfiguration(SenderConfiguration config) {
  if (String.IsNullOrEmpty(config.Url)) {
      Console.WriteLine("ERROR: service Url not specified");
      return false;
  }
  if (String.IsNullOrEmpty(config.ApiKey)) {
      Console.WriteLine("ERROR: ApiKey not specified");
      return false;
  }
  if (String.IsNullOrEmpty(config.ApiKey)) {
      Console.WriteLine("ERROR: ReportFileName not specified");
      return false;
  }
  ....
}

Unity C# reference source code

V3021 CWE-561 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 CustomScriptAssembly.cs 179


public bool IsCompatibleWith(....)
{
  ....
  if (buildingForEditor)
    return IsCompatibleWithEditor();

  if (buildingForEditor)
    buildTarget = BuildTarget.NoTarget; // Editor
  ....
}

.NET Core Libraries (CoreFX)

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 XMLDiffLoader.cs 301


private int ReadOldRowData(
  DataSet ds, ref DataTable table, ref int pos, XmlReader row)
{
  ....
  if (table == null)
  {
    row.Skip();
    return -1;
  }
  ....

  if (table == null)
    throw ExceptionBuilder.DiffgramMissingTable(
            XmlConvert.DecodeName(row.LocalName));
  ....
}

OpenCvSharp

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 InputArray.cs 394


private static MatType EstimateType(Type t)
{
  ....
  if (t == typeof(Vec2b))
    return MatType.CV_8UC2;
  if (t == typeof(Vec3b))
    return MatType.CV_8UC3;
  if (t == typeof(Vec4b))
    return MatType.CV_8UC4;
  if (t == typeof(Vec6b))
    return MatType.CV_8UC(6);
  if (t == typeof(Vec2s))         // <=
    return MatType.CV_16SC2;
  ....
  if (t == typeof(Vec2s))         // <=
    return MatType.CV_32SC2;
  ....
}

OpenCvSharp

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 Cv2_calib3d.cs 1370


public static double CalibrateCamera(....)
{
  if (objectPoints == null)
    throw new ArgumentNullException(nameof(objectPoints));
  if (objectPoints == null)
    throw new ArgumentNullException(nameof(objectPoints));
  ....
}

Also see V3022

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 Cv2_calib3d.cs 1444

Emby

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 SkiaEncoder.cs 537


public string EncodeImage(string inputPath,
                          DateTime dateModified,
                          string outputPath,
                          bool autoOrient,
                          ImageOrientation? orientation,
                          int quality,
                          ImageProcessingOptions options,
                          ImageFormat selectedOutputFormat)
{
  if (string.IsNullOrWhiteSpace(inputPath))
  {
      throw new ArgumentNullException("inputPath");
  }
  if (string.IsNullOrWhiteSpace(inputPath))
  {
      throw new ArgumentNullException("outputPath");
  }
  ....
}

Nethermind

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 Nethermind.Network.Benchmark InFlowBenchmarks.cs 55


public void Setup()
{
  if (_decoderBuffer.ReadableBytes > 0)
  {
    throw new Exception("decoder buffer");
  }

  if (_decoderBuffer.ReadableBytes > 0)
  {
    throw new Exception("decoder buffer");
  }
  ....
}

ONLYOFFICE Community Server

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 WikiEngine.cs 688


private static LinkType CheckTheLink(string str, out string sLink)
{
    sLink = string.Empty;

    if (string.IsNullOrEmpty(str))
        return LinkType.None;

    if (str[0] == '[')
    {
        sLink = str.Trim("[]".ToCharArray()).Split('|')[0].Trim();
    }
    else if (....)
    {
        sLink = str.Split('|')[0].Trim();
    }
    sLink = sLink.Split('#')[0].Trim();    // <=
    if (string.IsNullOrEmpty(str))         // <=
        return LinkType.None;

    if (sLink.Contains(":"))
    {
      ....
    }
    ....
}

Umbraco

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 ObjectExtensions.cs 615


public static string ToXmlString(this object value, Type type)
{
  ....
  if (type == typeof(bool)) return XmlConvert.ToString((bool)value);
  ....
  if (type == typeof(bool)) return XmlConvert.ToString((bool)value);
  ....
}

.NET 6 libraries

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 DeflateManagedStream.cs 27


internal DeflateManagedStream(Stream stream,
                              ZipArchiveEntry.CompressionMethodValues method,
                              long uncompressedSize = -1)
{
  if (stream == null)
    throw new ArgumentNullException(nameof(stream));
  if (!stream.CanRead)
    throw new ArgumentException(SR.NotSupported_UnreadableStream,
                                nameof(stream));
  if (!stream.CanRead)
    throw new ArgumentException(SR.NotSupported_UnreadableStream,
                                nameof(stream));

  ....
}

.NET 6 libraries

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 JsonSerializer.Read.String.cs 163


public static object? Deserialize(ReadOnlySpan<char> json,
                                  Type returnType,
                                  JsonSerializerOptions? options = null)
{
  // default/null span is treated as empty
  if (returnType == null)
  {
    throw new ArgumentNullException(nameof(returnType));
  }

  if (returnType == null)
  {
    throw new ArgumentNullException(nameof(returnType));
  }

  JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
  return ReadFromSpan<object?>(json, jsonTypeInfo)!;
}

.NET 6 libraries

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 XmlSerializationWriterILGen.cs 102


private void WriteQualifiedNameElement(....)
{
  bool hasDefault = defaultValue != null && defaultValue != DBNull.Value;
  if (hasDefault)
  {
    throw Globals.NotSupported(
      "XmlQualifiedName DefaultValue not supported.  Fail in WriteValue()");
  }
  ....
  if (hasDefault)
  {
    throw Globals.NotSupported(
      "XmlQualifiedName DefaultValue not supported.  Fail in WriteValue()");
  }
}

Barotrauma

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 DebugConsole.cs 2177


private static void InitProjectSpecific()
{
  ....
  AssignOnClientRequestExecute(
    "setclientcharacter",
    (Client senderClient, Vector2 cursorWorldPos, string[] args) =>
    {
      if (args.Length < 2)
      {
        GameMain.Server.SendConsoleMessage("....", senderClient);
        return;
      }

      if (args.Length < 2)
      {
        ThrowError("....");
        return;
      }
    );
  ....
}

GrandNode

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 PictureService.cs 835


public enum SKEncodedImageFormat
{
 ....
 Wbmp = 5,
 Webp = 6,
 ....
}

protected SKEncodedImageFormat EncodedImageFormat(string mimetype)
{
 SKEncodedImageFormat defaultFormat = SKEncodedImageFormat.Jpeg;
 if (string.IsNullOrEmpty(mimetype))
  return defaultFormat;

 mimetype = mimetype.ToLower();
 if (   mimetype.Contains("jpeg")
     || mimetype.Contains("jpg")
     || mimetype.Contains("pjpeg"))
  return defaultFormat;

 if (mimetype.Contains("png"))
  return SKEncodedImageFormat.Png;

 if (mimetype.Contains("webp"))      // <=
  return SKEncodedImageFormat.Webp;

 if (mimetype.Contains("webp"))      // <=
  return SKEncodedImageFormat.Webp;

 if (mimetype.Contains("gif"))
  return SKEncodedImageFormat.Gif;

 //if mime type is BMP format then happens error with convert picture
 if (mimetype.Contains("bmp"))
  return SKEncodedImageFormat.Png;

 if (mimetype.Contains("ico"))
  return SKEncodedImageFormat.Ico;

 return defaultFormat;
}