Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug No null propagation warning for unity objects when we use method

Discussion in 'Scripting' started by Slash321, Sep 24, 2020.

  1. Slash321

    Slash321

    Joined:
    Jun 28, 2018
    Posts:
    8
    As we know from this blog https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/ we cant use null propagation for Unity objects.
    Rider and VS show warning when we try to use null propagation for unity, but there is no any warnings when we use null propagation for methods which return unity objects. I use Unity 2018.4.16f1

    Here is example:

    Code (CSharp):
    1.  
    2. public class BaseTestView : MonoBehaviour
    3. {
    4.     public string GetName()
    5.     {
    6.         return this.name;
    7.     }
    8.    
    9. }
    10.  
    11. public class PropagationTest
    12. {
    13.     public GameObject tartgetGo;
    14.     void Main()
    15.     {
    16.         var baseTestView = GetBaseTestView();
    17.         baseTestView?.GetName(); //Warning
    18.         GetBaseTestView()?.GetName(); // No warning
    19.         tartgetGo.GetComponent<BaseTestView>()?.GetName(); // No warning
    20.     }
    21.  
    22.     public BaseTestView GetBaseTestView()
    23.     {
    24.         return tartgetGo.GetComponent<BaseTestView>();
    25.     }
    26. }
    27.  
    Is it a bug or may be I need to setup code analyser in some way?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    I have no idea... I would just write code that didn't rely on this new ? collapsing operator, simply based upon how many dozens of posts I've seen where people have issues and confusion about it.

    I prefer to try and keep my projects free of unnecessary issues and confusion! I add plenty of that all by myself without newfangled language shortcuts! :)
     
    Suddoha likes this.
  3. Slash321

    Slash321

    Joined:
    Jun 28, 2018
    Posts:
    8
    Main reason of analyser warnings and errors is to prevent user from making mistakes, so it not question of code quality or something.

    So if it doesn't work in some cases it could be a bug, also there is a possibility that I need to add some additional changes to rule set or analyser.
     
    Last edited: Sep 25, 2020
    Beetruth likes this.