[mcs] Warn about unused local constants
authorMarek Safar <marek.safar@gmail.com>
Tue, 20 Sep 2016 08:42:17 +0000 (10:42 +0200)
committerMarek Safar <marek.safar@gmail.com>
Tue, 20 Sep 2016 09:05:39 +0000 (11:05 +0200)
mcs/errors/cs0219-7.cs [new file with mode: 0644]
mcs/mcs/statement.cs

diff --git a/mcs/errors/cs0219-7.cs b/mcs/errors/cs0219-7.cs
new file mode 100644 (file)
index 0000000..ca40a59
--- /dev/null
@@ -0,0 +1,11 @@
+// CS0219: The constant `C' is never used
+// Line: 9
+// Compiler options: -warn:3 -warnaserror
+
+class C
+{
+       public static void Main ()
+       {
+               const int C = 1;
+       }
+}
\ No newline at end of file
index 6b658945ff30c30261efeadb5015965883c92a6a..b644e126dc1cecfab235cb03d5bac052d7c0b74e 100644 (file)
@@ -2280,6 +2280,9 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
+                       if (!Variable.IsUsed)
+                               ec.Report.Warning (219, 3, loc, "The constant `{0}' is never used", Variable.Name);
+                       
                        // Nothing to emit, not even sequence point
                }
 
@@ -2438,6 +2441,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public bool IsUsed {
+                       get {
+                               return (flags & Flags.Used) != 0;
+                       }
+               }
+
                public bool IsFixed {
                        get {
                                return (flags & Flags.FixedVariable) != 0;
@@ -2527,8 +2536,10 @@ namespace Mono.CSharp {
 
                public Expression CreateReferenceExpression (ResolveContext rc, Location loc)
                {
-                       if (IsConstant && const_value != null)
+                       if (IsConstant && const_value != null) {
+                               SetIsUsed ();
                                return Constant.CreateConstantFromValue (Type, const_value.GetValue (), loc);
+                       }
 
                        return new LocalVariableReference (this, loc);
                }