Skip to content

Commit

Permalink
Put param/var names in linter messages where "Unused" (Azure#2899)
Browse files Browse the repository at this point in the history
* WiP: Adding identifier name to Param Unused message for linter

* Add unused variable name to linter warning message

* Update test baselines

* Fix up tests and baselines to expect named param/var in linter warning message

Co-authored-by: jofleish <[email protected]>
Co-authored-by: Bicep Automation <[email protected]>
  • Loading branch information
3 people committed Jun 1, 2021
1 parent b186879 commit bf46a9c
Show file tree
Hide file tree
Showing 37 changed files with 741 additions and 651 deletions.
6 changes: 3 additions & 3 deletions src/Bicep.Core.IntegrationTests/DecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void ParameterDecorator_AttachedToOtherKindsOfDeclarations_CannotBeUsedAs
{
diagnosticsByFile[mainUri].Should().HaveDiagnostics(new[] {
("BCP126", DiagnosticLevel.Error, "Function \"maxLength\" cannot be used as a variable decorator."),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("foo")),
("BCP127", DiagnosticLevel.Error, "Function \"allowed\" cannot be used as a resource decorator."),
("BCP128", DiagnosticLevel.Error, "Function \"secure\" cannot be used as a module decorator."),
("BCP129", DiagnosticLevel.Error, "Function \"minValue\" cannot be used as an output decorator."),
Expand Down Expand Up @@ -166,9 +166,9 @@ public void NonDecoratorFunction_AttachedToDeclaration_CannotBeUsedAsDecorator()
{
diagnosticsByFile[mainUri].Should().HaveDiagnostics(new[] {
("BCP152", DiagnosticLevel.Error, "Function \"resourceId\" cannot be used as a decorator."),
(ParametersMustBeUsedRule.Code, DiagnosticLevel.Warning, new ParametersMustBeUsedRule().GetMessage()),
(ParametersMustBeUsedRule.Code, DiagnosticLevel.Warning, new ParametersMustBeUsedRule().GetMessage("foo")),
("BCP152", DiagnosticLevel.Error, "Function \"concat\" cannot be used as a decorator."),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("bar")),
("BCP152", DiagnosticLevel.Error, "Function \"environment\" cannot be used as a decorator."),
("BCP152", DiagnosticLevel.Error, "Function \"union\" cannot be used as a decorator."),
("BCP152", DiagnosticLevel.Error, "Function \"guid\" cannot be used as a decorator."),
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core.IntegrationTests/DeployTimeConstantTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void DtcValidation_EntireResourceOrModuleAccessAtInvalidLocations_Produce
");
result.Should().HaveDiagnostics(new[]
{
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("foo")),
("BCP178", DiagnosticLevel.Error, "This expression is being used in the for-expression, which requires a value that can be calculated at the start of the deployment. Properties of dnsZone which can be calculated at the start include \"apiVersion\", \"id\", \"name\", \"type\"."),
("BCP178", DiagnosticLevel.Error, "This expression is being used in the for-expression, which requires a value that can be calculated at the start of the deployment. Properties of storageAccounts which can be calculated at the start include \"apiVersion\", \"id\", \"name\", \"type\"."),
("BCP178", DiagnosticLevel.Error, "This expression is being used in the for-expression, which requires a value that can be calculated at the start of the deployment. Properties of aRecord which can be calculated at the start include \"apiVersion\", \"id\", \"name\", \"type\"."),
Expand Down
24 changes: 12 additions & 12 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ param l
("BCP028", DiagnosticLevel.Error, "Identifier \"l\" is declared multiple times. Remove or rename the duplicates."),
("BCP079", DiagnosticLevel.Error, "This expression is referencing its own declaration, which is not allowed."),
("BCP028", DiagnosticLevel.Error, "Identifier \"l\" is declared multiple times. Remove or rename the duplicates."),
(ParametersMustBeUsedRule.Code, DiagnosticLevel.Warning, new ParametersMustBeUsedRule().GetMessage()),
(ParametersMustBeUsedRule.Code, DiagnosticLevel.Warning, new ParametersMustBeUsedRule().GetMessage("l")),
("BCP014", DiagnosticLevel.Error, "Expected a parameter type at this location. Please specify one of the following types: \"array\", \"bool\", \"int\", \"object\", \"string\"."),
});
}
Expand Down Expand Up @@ -361,7 +361,7 @@ public void Test_Issue1332()
");

result.Should().HaveDiagnostics(new[] {
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage())
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("issue"))
});
result.Template.Should().HaveValueAtPath("$.variables.issue", "[if(true(), createObject('prop1', createObject(variables('propname'), createObject())), createObject())]");
}
Expand Down Expand Up @@ -906,7 +906,7 @@ public void Test_Issue1817()
result.Should().HaveDiagnostics(new[] {
("BCP032", DiagnosticLevel.Error, "The value must be a compile-time constant."),
("BCP057", DiagnosticLevel.Error, "The name \"w\" does not exist in the current context."),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("foo")),
});
}

Expand All @@ -929,8 +929,8 @@ public void Test_Issue1630()
");

result.Should().HaveDiagnostics(new[] {
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("firstApiVersion")),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("firstResourceFirstApiVersion")),
});
result.Template.Should().HaveValueAtPath("$.variables['singleResource']", "[providers('Microsoft.Insights', 'components')]");
result.Template.Should().HaveValueAtPath("$.variables['firstApiVersion']", "[variables('singleResource').apiVersions[0]]");
Expand Down Expand Up @@ -964,7 +964,7 @@ public void Test_Issue1627()

result.Should().HaveDiagnostics(new[] {
("BCP104", DiagnosticLevel.Error, "The referenced module has errors."),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("bar")),
});
}

Expand Down Expand Up @@ -1558,7 +1558,7 @@ public void Variable_loops_should_not_cause_infinite_recursion()
");

result.Should().HaveDiagnostics(new[]{
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("arrayOfObjectsViaLoop")),
});
result.Template.Should().NotBeNull();
}
Expand Down Expand Up @@ -1934,7 +1934,7 @@ public void Test_Issue2484()
");

result.Should().HaveDiagnostics(new[] {
(ParametersMustBeUsedRule.Code, DiagnosticLevel.Warning, new ParametersMustBeUsedRule().GetMessage()),
(ParametersMustBeUsedRule.Code, DiagnosticLevel.Warning, new ParametersMustBeUsedRule().GetMessage("foo")),
("BCP027", DiagnosticLevel.Error, "The parameter expects a default value of type \"'apple' | 'banana'\" but provided value is of type \"'peach'\"."),
});
}
Expand Down Expand Up @@ -2018,7 +2018,7 @@ public void Test_Issue2624()
");

result.Should().HaveDiagnostics(new[] {
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("foo")),
});
}

Expand Down Expand Up @@ -2091,7 +2091,7 @@ public void Test_Issue2248_UnionTypeInArrayAccessBaseExpression()
var primaryLocation = locations[0]
");
result.Should().HaveDiagnostics(new[] {
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("primaryLocation")),
});
}

Expand All @@ -2105,7 +2105,7 @@ public void Test_Issue2248_UnionTypeInArrayAccessBaseExpression_NegativeCase()
");
result.Should().HaveDiagnostics(new[]
{
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("primaryFoo")),
("BCP076",DiagnosticLevel.Error,"Cannot index over expression of type \"array | bool\". Arrays or objects are required.")
});
}
Expand All @@ -2127,7 +2127,7 @@ public void Test_Issue2248_UnionTypeInPropertyAccessBaseExpression()
var p = chosenOne.foo
");
result.Should().HaveDiagnostics(new[] {
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage()),
(UnusedVariableRule.Code, DiagnosticLevel.Warning, new UnusedVariableRule().GetMessage("p")),
});
}

Expand Down
14 changes: 5 additions & 9 deletions src/Bicep.Core.IntegrationTests/Semantics/SemanticModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using Bicep.Core.Diagnostics;
using Bicep.Core.FileSystem;
using Bicep.Core.Navigation;
using Bicep.Core.Samples;
using Bicep.Core.Semantics;
Expand All @@ -17,8 +9,12 @@
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;

namespace Bicep.Core.IntegrationTests.Semantics
{
Expand Down
7 changes: 3 additions & 4 deletions src/Bicep.Core.Samples/DataSet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core.Parsing;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand All @@ -8,10 +11,6 @@
using System.Linq;
using System.Reflection;
using System.Threading;
using Bicep.Core.Parsing;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bicep.Core.Samples
{
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core.Samples/Files/AKS_LF/main.diagnostics.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ param osDiskSizeGB int = 0
@minValue(1)
@maxValue(50)
param agentCount int = 3
//@[6:16) [no-unused-params (Warning)] Parameter is declared but never used. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-unused-params)) |agentCount|
//@[6:16) [no-unused-params (Warning)] Parameter "agentCount" is declared but never used. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-unused-params)) |agentCount|

param agentVMSize string = 'Standard_DS2_v2'
// osType was a defaultValue with only one allowedValue, which seems strange?, could be a good TTK test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var two = one.f
//@[10:13) [BCP080 (Error)] The expression is involved in a cycle ("one" -> "two"). (CodeDescription: none) |one|
// #completionTest(17) -> empty
var twotwo = one.
//@[4:10) [no-unused-vars (Warning)] Variable is declared but never used. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-unused-vars)) |twotwo|
//@[4:10) [no-unused-vars (Warning)] Variable "twotwo" is declared but never used. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-unused-vars)) |twotwo|
//@[13:16) [BCP080 (Error)] The expression is involved in a cycle ("one" -> "two"). (CodeDescription: none) |one|
//@[17:17) [BCP020 (Error)] Expected a function or property name at this location. (CodeDescription: none) ||

Expand Down
Loading

0 comments on commit bf46a9c

Please sign in to comment.