0

I am using T4 to generate some code. The code will be in a class called "MyClass.generated.cs" and typical output will look like this.

//<autogenerated/>
namespace MyNamespace
{
    using System;
    using System.CodeDom.Compiler;

    [GeneratedCode("T4", "1.0.0.0")]
    public partial class MyClass: SomeBaseClass
    {
        private SomeBaseClass myBackingField;
    }
}

However, even though the class is decorated with the GeneratedCodeAttribute, I still get a Code Analysis warning as follows:

Field 'MyNamespace.MyClass.myBackingField' is never assigned to, and will always have its default value null

I have ensure that the Project PropertiesCode Analysis → "Suppress results from generated code (managed only)" checkbox is checked.

Please note that I understand the meaning of the warning - I just want to know how to suppress it :)

Possible solutions I could modify my generator to use Suppressions to suppress specific warnings, but this is extra work that I shouldn't have to do (as generated code should be ignored by Code Analysis).

Related Questions

EDIT with background context The actual generated code is essentially a wrapper around SomeBaseClass. There are 100+ types in a namespace, and I want to change the behaviour of a subset of those. There are other warnings being generated as well - I just used this one as an example. Consider for example, if there is a property SomeBaseClass.MyObsoleteProperty, which is decorated with the ObsoleteAttribute. My code generater would still create a MyClass.MyObsoleteProperty which would raise a Code Analysis warning.

Another example would be where the SomeBaseClass (which is from a 3rd-party) would itself raise Code Analysis warnings if they had bothered to check for them (maybe the class is not CLS-compliant, for example). My wrapper will recreate any errors they have (and that would actually be the desired behaviour).

Community
  • 1
  • 1
RB.
  • 36,301
  • 12
  • 91
  • 131
  • Well *is* anything going to assign a value to `myBackingField`? Would it actually make sense for your generated code to have a constructor which sets the field? (And make it readonly at the same time.) – Jon Skeet Jul 05 '13 at 15:07
  • Hi Jon - basically, there are 100+ generated classes, and I'm going to be implementing partial classes for about half of them. For that half, yes, they will have the field set. The reason I generate all 100 is that I'm using a single T4 template to generate a class for every member of a specific namespace. – RB. Jul 05 '13 at 15:09
  • 1
    Well it sounds like you should either live with the warning, or go to the trouble of having two separate templates. After all, having a field you don't need in the other half of the classes is far from ideal. – Jon Skeet Jul 05 '13 at 15:11
  • @JonSkeet I've added a bit more explanation about what I'm doing to hopefully make it clearer why I want to suppress warnings - I've given some other examples of warnings I face that I hope make it clear I have to suppress, rather than fix... Regardless, Code Analysis *should* ignore generated code (the [docs](http://msdn.microsoft.com/en-us/library/dd742298.aspx) make that clear) so I don't understand why this is not working. – RB. Jul 05 '13 at 15:21
  • Thanks for the extra context - hopefully it'll be useful to others too. Unfortunately I don't have an answer for you :( – Jon Skeet Jul 05 '13 at 15:29
  • @JonSkeet I've stumped Jon Skeet :) That's made my day! Also, I figured it out in the end (see answer - I was being slightly thick), so double w00ts. – RB. Jul 05 '13 at 15:37
  • 1
    Well, now that we know that the premise of the question was wrong... you did explicitly state that it was a Code Analysis warning :) – Jon Skeet Jul 05 '13 at 15:39
  • @JonSkeet The first rule of Stack Overflow is don't trust the question setter :P. The second rule is "No smoking". – RB. Jul 05 '13 at 15:40

2 Answers2

2

I figured it out - this is not a Code Analysis warning - it's a compiler warning.

Therefore, the only way to disable it is to modify the generator to enclose the class in pragma directives to suppress compiler warnings, e.g

#pragma warning disable warning-list

// Now generate some code

#pragma warning restore warning-list

WARNING Note that this is a dangerous feature - compiler warnings are there for a reason! Try and limit your use of it to as small a section as possible.

More information can be found at Suppressing "is never used" and "is never assigned to" warnings in C#

List of compiler warnings and errors here.

Community
  • 1
  • 1
RB.
  • 36,301
  • 12
  • 91
  • 131
0

I think you mean

#pragma warning disable 
// generated code
#pragma warning restore

the "warning-list" is a placeholder in MSDN documentation for something like "c0605,c0403,c3498" etc

bc3tech
  • 1,228
  • 14
  • 27