Examples of errors detected by the V3019 diagnostic.

V3019. It is possible that an incorrect variable is compared with null after type conversion using 'as' keyword.


SharpDevelop

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'node', 'solutionFolderNode'. SolutionNodeCommands.cs 127


public override void Run()
{
  ....
  ISolutionFolderNode solutionFolderNode =
    node as ISolutionFolderNode;

  if (node != null)
  {
    ISolutionFolder newSolutionFolder =
      solutionFolderNode.Folder.CreateFolder(....);
    solutionFolderNode.Solution.Save();
  ....
}

Identical errors can be found in some other places:

  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'geometry', 'g'. PathHandlerExtension.cs 578
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'oldTransform', 'tg'. ModelTools.cs 420
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'node', 'solutionFolderNode'. SolutionNodeCommands.cs 104

Microsoft Code Contracts

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'other', 'right'. CallerInvariant.cs 189


public override Predicate JoinWith(Predicate other)
{
  var right = other as PredicateNullness;
  if (other != null)
  {
    if (this.value == right.value)
    {
      return this;
    }
  }

  return PredicateTop.Value;
}

Identical errors can be found in some other places:

  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'facts', 'moreRefinedFacts'. SimplePostconditionDispatcher.cs 319
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'objProvenance', 'provenance'. AssertionCrawlerAnalysis.cs 816
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'prev', 'other'. NonRelationalValueAbstraction.cs 1063
  • And 7 additional diagnostic messages.

.NET Core Libraries (CoreFX)

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'comparand', 'comparedCredentialKey'. CredentialCache.cs 4007


public override bool Equals(object comparand)
{
  CredentialHostKey comparedCredentialKey =
                                  comparand as CredentialHostKey;

  if (comparand == null)
  {
    // This covers also the compared == null case
    return false;
  }

  bool equals = string.Equals(AuthenticationType,
        comparedCredentialKey.AuthenticationType, ....
  ....
}

Identical errors can be found in some other places:

  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'comparand', 'comparedCredentialKey'. CredentialCache.cs 497
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'myObject', 'myString'. CaseInsensitiveAscii.cs 46
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'a', 'nodeA'. AttributeSortOrder.cs 22
  • And 3 additional diagnostic messages.

MonoDevelop

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'o', 'sr'. MonoDevelop.Core SolutionItemReference.cs 81


public override bool Equals (object o)
{
  SolutionItemReference sr = o as SolutionItemReference;
  if (o == null)
    return false;
  return (path == sr.path) && (id == sr.id);
}

MonoDevelop

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'sender', 'selection'. MonoDevelop.Ide TasksOptionsPanel.cs 123


void OnTokenSelectionChanged (object sender, EventArgs args)
{
  TreeSelection selection = sender as TreeSelection;
  if (sender != null)
  {
    TreeIter iter;
    TreeModel model = (TreeModel)tokensStore;
    if (selection.GetSelected (out model, out iter)) {
        entryToken.Text = (string)tokensStore.GetValue (iter, 0);
        comboPriority.Active = (int)tokensStore
                                      .GetValue (iter, 1);
    } else
    {
      entryToken.Text = String.Empty;
      comboPriority.Active = (int)TaskPriority.Normal;
    }
  }
}

Identical errors can be found in some other places:

  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'data', 'urlMarker'. MonoDevelop.SourceEditor MarkerOperationsHandler.cs 43
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'symbol', 'method'. CSharpBinding FormatStringHelper.cs 59

IronPython and IronRuby

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'value', 'strValue'. EnvironmentSingletonOps.cs 189


public static bool HasValue(RubyContext/*!*/ context,
  object/*!*/ self, object value) {
  var strValue = value as MutableString;
  if (value == null) {
    return false;
  }
  var clrStrValue = strValue.ConvertToString();
  ....
}

IronPython and IronRuby

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'node', 'scalar'. RubyConstructor.cs 230


private static RubyRegex/*!*/ ConstructRubyRegexp(
  RubyConstructor/*!*/ ctor, Node/*!*/ node) {
  ScalarNode scalar = node as ScalarNode;
  if (node == null) {
    throw RubyExceptions.CreateTypeError(
      "Can only create regex from scalar node");
  }
  Match match = _regexPattern.Match(scalar.Value);
  ....
}

.NET Compiler Platform ("Roslyn")

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'obj', 'd'. DiagnosticDescription.cs 201


public override bool Equals(object obj)
{
  var d = obj as DiagnosticDescription;

  if (obj == null)
    return false;

  if (!_code.Equals(d._code))
    return false;
  ....
}

.NET Compiler Platform ("Roslyn")

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'other', 'otherResourceString'. LocalizableResourceString.cs 121


protected override bool AreEqual(object other)
{
  var otherResourceString = other as LocalizableResourceString;
  return
    other != null &&
    _nameOfLocalizableResource ==
      otherResourceString._nameOfLocalizableResource &&
    _resourceManager == otherResourceString._resourceManager &&
    _resourceSource == otherResourceString._resourceSource &&
    ....
}

.NET Compiler Platform ("Roslyn")

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'parentStartTag', 'parentElement'. XmlTagCompletionCommandHandler.cs 123


private bool HasMatchingEndTag(
  XmlElementStartTagSyntax parentStartTag)
{
  if (parentStartTag == null)
  {
    return false;
  }

  var parentElement = parentStartTag.Parent as XmlElementSyntax;
  if (parentStartTag == null)
  {
    return false;
  }
  var endTag = parentElement.EndTag;
  ....
}

This error can also be detected in a different way. The analyzer generates such a message for this code fragment: 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 XmlTagCompletionCommandHandler.cs 117


Sony ATF

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'd', 'box'. Atf.Gui.Wpf.vs2010 PasswordBoxBehavior.cs 38


private static void OnBoundPasswordChanged(DependencyObject d,
                      DependencyPropertyChangedEventArgs e)
{
    PasswordBox box = d as PasswordBox;

    if (d == null || !GetBindPassword(d))
    {
        return;
    }

    box.PasswordChanged -= HandlePasswordChanged;
    ....
}

Space Engineers

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'builder', 'ob'. Sandbox.Game MyWeaponBlockDefinition.cs 21


protected override void Init(
                         MyObjectBuilder_DefinitionBase builder)
{
  base.Init(builder);

  var ob = builder as MyObjectBuilder_WeaponBlockDefinition;
  Debug.Assert(builder != null);                           // <=

  WeaponDefinitionId = new MyDefinitionId(
                         ob.WeaponDefinitionId.Type,
                         ob.WeaponDefinitionId.Subtype);
  ResourceSinkGroup = MyStringHash.GetOrCompute(
                        ob.ResourceSinkGroup);
  InventoryMaxVolume = ob.InventoryMaxVolume;
}

Space Engineers

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'item', 'actionsItem'. Sandbox.Game MyGuiControlToolbar.cs 511


private void contextMenu_ItemClicked(....)
{
  ....
  var actionsItem = item as MyToolbarItemActions;
  if (item != null)                                       // <=
  {
    if (idx < 0 || idx >= actionsItem
                          .PossibleActions(ShownToolbar
                                             .ToolbarType)
                          .Count)
      RemoveToolbarItem(slot);
  ....
  }
  ....
}

Identical errors can be found in some other places:

  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'ob', 'objectBuilder'. Sandbox.Game MyBlockNavigationDefinition.cs 172
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'Owner', 'character'. Sandbox.Game MyWelder.cs 232

Old NASA World Wind (C#)

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'obj', 'robj'. RenderableObject.cs 199


public int CompareTo(object obj)
{
  RenderableObject robj = obj as RenderableObject;
  if(obj == null)   // <=
    return 1;
  return this.m_renderPriority.CompareTo(robj.RenderPriority);
}

FlashDevelop

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'item', 'val'. WizardHelper.cs 67


public static void SetControlValue(....)
{
  ....
  string val = item as string;
  if (item == null) continue;
  ....
}

MSBuild

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'obj', 'name'. AssemblyRemapping.cs 64


public override bool Equals(object obj)
{
   AssemblyNameExtension name = obj as AssemblyNameExtension;
   if (obj == null)  // <=
   {
     return false;
   }
   ....
}

Mono

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'o', 'umc'. UrlMembershipCondition.cs 111


public override bool Equals (object o)
{
  UrlMembershipCondition umc = (o as UrlMembershipCondition);
  if (o == null)                                      // <=
    return false;

  ....

  return (String.Compare (u, 0, umc.Url, ....) == 0); // <=
}

Mono

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'v1', 'cmp'. Array.cs 1487


static bool QSortArrange (.... ref object v0, int hi,
                          ref object v1, ....)
{
  IComparable cmp;
  ....
  cmp = v1 as IComparable;

  if (v1 == null || cmp.CompareTo (v0) < 0) {
    ....
  }
  ....
}

Microsoft Bot Builder

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'step', 'prop'. FieldReflector.cs 231


protected Type StepType(object step)
{
  var field = step as FieldInfo;
  var prop = step as PropertyInfo;
  return (step == null ? null :
         (field == null ? prop.PropertyType :  // <=
                          field.FieldType));
}

PowerShell

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'j', 'child'. System.Management.Automation Job.cs 1876


internal List<Job> GetJobsForComputer(String computerName)
{
  ....
  foreach (Job j in ChildJobs)
  {
    PSRemotingChildJob child = j as PSRemotingChildJob;
    if (j == null) continue;
    if (String.Equals(child.Runspace
                           .ConnectionInfo
                           .ComputerName,
                      computerName,
                      StringComparison.OrdinalIgnoreCase))
    {
      returnJobList.Add(child);
    }
  }
  return returnJobList;
}

Identical errors can be found in some other places:

  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'j', 'child'. System.Management.Automation Job.cs 1900
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'j', 'child'. System.Management.Automation Job.cs 1923

Media Portal 2

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'obj', 'other'. EpisodeInfo.cs 560


public override bool Equals(object obj)
{
  EpisodeInfo other = obj as EpisodeInfo;
  if (obj == null) return false;
  if (TvdbId > 0 && other.TvdbId > 0)
    return TvdbId == other.TvdbId;
  ....
}

PascalABC.NET

V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'baseScope', 'this.baseScope'. CodeCompletion SymTable.cs 3497


public TypeScope(...., SymScope baseScope)
{
  ....
  this.baseScope = baseScope as TypeScope;
  ....
  if (baseScope == null)
  {
    ....
  }
  ....
}

Identical errors can be found in some other places:

  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'returned_scope', 'ts'. CodeCompletion ExpressionVisitor.cs 1595
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'returned_scope', 'tmp_scope'. CodeCompletion DomSyntaxTreeVisitor.cs 1553
  • V3019 Possibly an incorrect variable is compared to null after type conversion using 'as' keyword. Check variables 'returned_scope', 'ts.elementType'. CodeCompletion DomSyntaxTreeVisitor.cs 2815
  • And 6 additional diagnostic messages.


Do you make errors in the code?

Check your code
with PVS-Studio

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

goto PVS-Studio;