Coder Social home page Coder Social logo

csharp-inconsistent-warning-ca1062's Introduction

csharp-inconsistent-warning-ca1062

This repository is related to issue 1416138.

Assigning an argument specified as a reference type (e.g. string) to an instance field (both in the constructor and in a plain method) does not trigger a CA1062 warning about a potential nullability issue.

The compiler only generates these two warnings:

warning CA1062: In externally visible method 'Foo.Foo(string x)', validate parameter 'x' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.

warning CA1062: In externally visible method 'void Foo.SetX(string x)', validate parameter 'x' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.

    public class Foo
    {
        public Foo(string x)
        {
            this.x = x; // does not warn
            this.len = x.Length; // warns (OK)
        }

        public void SetX(string x)
        {
            this.x = x; // does not warn
            this.len = x.Length; // warns (OK)
        }

        public string X => this.x;
        public int Len => this.len;

        private string x;
        private int len;
    }

I am expecting that if I assign x to this.x without checking that x is indeed not null, then I have the risk of using this.x in cases where it may not be null.

Context

The warnings are generated when following settings are used:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <LangVersion>latest</LangVersion>
    <Nullable>enable</Nullable>
    <EnableNETAnalyzers>true</EnableNETAnalyzers> <!-- enable analyzers -->
    <AnalysisMode>AllEnabledByDefault</AnalysisMode> <!-- enable ALL analyzers -->
  </PropertyGroup>
</Project>

csharp-inconsistent-warning-ca1062's People

Contributors

epsitec avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.