To get a trial key
fill out the form below
Team License (standard version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
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.

>
>
>
V3153. Dereferencing the result of null…
Analyzer Diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Diagnosis of micro-optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
MISRA errors
AUTOSAR errors
Additional information
Contents

V3153. Dereferencing the result of null-conditional access operator can lead to NullReferenceException. Consider removing parentheses around null-conditional access expression.

Feb. 19, 2020

The analyzer has detected a potential error that can result in a null dereference. The result of applying the conditional access operator to a potentially null element is enclosed in parentheses before further dereference.

This means one of the two scenarios:

1) An error will occur if the object being accessed is an empty reference.

2) The expression always evaluates correctly because the object the conditional access operator is applied to is never equal to null. In this case, the check is unnecessary.

Consider the following example. Executing this code may lead to throwing an exception:

var t = (obj?.ToString()).GetHashCode();

If 'obj' happens to have the value null, the 'ToString()' method of the 'obj?.ToString()' expression will not be called – this is how the conditional access operator works. However, the call to the 'GetHashCode()' method will be executed anyway because it is not affected by the conditional access operator.

Fixed code:

var t = obj?.ToString().GetHashCode();

We have not only eliminated the unsafe dereference but also made sure that the 't' variable is of type 'Nullable<int>', which correctly represents its contents as potentially null.

The next example demonstrates a case where the dereference is safe and the check is not needed:

object obj = GetNotNullString();
....
var t = ((obj as String)?.Length).GetHashCode();

This expression evaluates correctly every time. The 'obj' object is always of type 'String', which means the check following the conversion is redundant.

Fixed version:

var t = ((String)obj).Length.GetHashCode();

This diagnostic is classified as:

This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept