Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ CompilationStartAnalysisContext context
OperationKind.Conversion
);

context.RegisterOperationAction(
ctx => AnalyzeCompoundAssignment(
ctx,
(ICompoundAssignmentOperation)ctx.Operation,
constantAttribute
),
OperationKind.CompoundAssignment
);

context.RegisterOperationAction(
ctx => AnalyzeMethodReference(
ctx,
Expand Down Expand Up @@ -177,6 +186,39 @@ INamedTypeSymbol constantAttribute
);
}

private static void AnalyzeCompoundAssignment(
OperationAnalysisContext context,
ICompoundAssignmentOperation compoundAssignment,
INamedTypeSymbol constantAttribute
) {

// Get implicit operator for the compound assignment
IMethodSymbol @operator = compoundAssignment.OutConversion.MethodSymbol;
if( @operator is null ) {
return;
}
if( @operator.Parameters.Length != 1 ) {
context.ReportDiagnostic(
descriptor: Diagnostics.UnexpectedNumberOfParametersForImplicitOperator,
location: compoundAssignment.Value.Syntax.GetLocation()
);
return;
}

// Operator parameter is not [Constant], so do nothing
IParameterSymbol parameter = @operator.Parameters[0];
if( !HasAttribute( parameter, constantAttribute ) ) {
return;
}

// Compound assignment with operator parameter that has [Constant] cannot be constant, so report it
context.ReportDiagnostic(
descriptor: Diagnostics.NonConstantPassedToConstantParameter,
location: compoundAssignment.Value.Syntax.GetLocation(),
messageArgs: new[] { parameter.Name }
);
}

private static void AnalyzeMethodReference(
OperationAnalysisContext context,
IMethodReferenceOperation operation,
Expand Down
9 changes: 9 additions & 0 deletions src/D2L.CodeStyle.Analyzers/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -765,5 +765,14 @@ public static class Diagnostics {
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
);

public static readonly DiagnosticDescriptor UnexpectedNumberOfParametersForImplicitOperator = new DiagnosticDescriptor(
id: "D2L0104",
title: "Unexpected number of parameters for implicit operator",
messageFormat: "The implicit operator has an unexpected number of parameters. Update analyzer to handle this case.",
category: "Safety",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,45 @@ string untrusted
{ Types.ConstantStruct v = trusted; }
{ Types.ConstantStruct v = /* NonConstantPassedToConstantParameter(value) */ variable /**/; }
{ Types.ConstantStruct v = /* NonConstantPassedToConstantParameter(value) */ untrusted /**/; }
{
Types.ConstantStruct v = Constants.String;
v += /* NonConstantPassedToConstantParameter(value) */ "abc" /**/;
}
{
Types.ConstantStruct v = Constants.String;
v += /* NonConstantPassedToConstantParameter(value) */ trusted /**/;
}
{
Types.ConstantStruct v = Constants.String;
v += /* NonConstantPassedToConstantParameter(value) */ variable /**/;
}
{
Types.ConstantStruct v = Constants.String;
v += /* NonConstantPassedToConstantParameter(value) */ "a" + "b" /**/;
}

{ Types.NonConstantStruct v = "abc"; }
{ Types.NonConstantStruct v = Constants.String; }
{ Types.NonConstantStruct v = constant; }
{ Types.NonConstantStruct v = trusted; }
{ Types.NonConstantStruct v = variable; }
{ Types.NonConstantStruct v = untrusted; }
{
Types.NonConstantStruct v = Constants.String;
v += "abc";
}
{
Types.NonConstantStruct v = Constants.String;
v += trusted;
}
{
Types.NonConstantStruct v = Constants.String;
v += variable;
}
{
Types.NonConstantStruct v = Constants.String;
v += "a" + "b";
}
}

#endregion
Expand Down
Loading