Skip to content
Merged
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
7 changes: 2 additions & 5 deletions lode/dotnet/analyzers.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# .NET 10 Roslyn Analyzers
*Updated: 2026-11-16T16:20:12Z*
*Updated: 2026-11-17T00:00:52Z*

## Strategy: Deny by Default

Expand Down Expand Up @@ -33,16 +33,13 @@ In `Directory.Build.props`:
```xml
<AnalysisLevel>latest-all</AnalysisLevel>
<AnalysisMode>All</AnalysisMode>
<AnalysisModeSecurity>All</AnalysisModeSecurity>
<AnalysisModeReliability>All</AnalysisModeReliability>
<AnalysisModePerformance>All</AnalysisModePerformance>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
```

Key interactions:
- `AnalysisLevel=latest-all` and `AnalysisMode=All` both select mode `All` in every category. The security, reliability, and performance category lines repeat that mode. They state the AI SDLC Readier policy explicitly. Do not set `AnalysisMode` below `All`, because the CA rules that are off in `Recommended` would stop running.
- `AnalysisLevel=latest-all` and `AnalysisMode=All` both select mode `All` in every category. The AI SDLC Readier policy for the security, reliability, and performance categories needs no separate category line. Do not set `AnalysisMode` below `All`, because the CA rules that are off in `Recommended` would stop running.
- `EnforceCodeStyleInBuild` makes IDE rules (IDE*) fire during `dotnet build`
- `TreatWarningsAsErrors` promotes all enabled warnings to errors

Expand Down
10 changes: 5 additions & 5 deletions lode/dotnet/analyzers/code-fix-output.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Code Fix Output
*Updated: 2026-11-03T03:14:50Z*
*Updated: 2026-11-17T03:01:43Z*

Rules for the text a fix writes and the text around it. A fix that emits a
broken member costs more than a missing fix: the apply path writes it into the
Expand Down Expand Up @@ -36,10 +36,10 @@ registers, so every offered fix changes code.

## Text in an Interpolated String

An `InterpolatedStringTextToken` carries two texts. `Text` is source text, so
escape it. `ValueText` is the runtime value, so keep it raw. Set both to the
escaped text and a tree comparison against parsed source fails. Set both raw
and a quote or a line break breaks the emitted literal.
An `InterpolatedStringTextToken` carries source text and value text. Escape
backslashes, quotes, and line breaks in `Text`. Keep the original literal run in
`ValueText`. Roslyn keeps doubled braces in both fields and combines each
literal run into one token. The binder resolves escaped braces later.

## Place a New Node with the Formatter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,60 +108,76 @@ private static async Task<Document> ApplyFixAsync(
ExpressionSyntax[] arguments)
{
var contents = new List<InterpolatedStringContentSyntax>();
var literalStart = 0;
var i = 0;

while (i < formatString.Length)
{
if (formatString[i] == '{')
{
i = ProcessOpenBrace(formatString, i, arguments, contents);
if (i < 0)
if (i + 1 < formatString.Length && formatString[i + 1] == '{')
{
return null;
i += DoubledBraceLength;
continue;
}
}
else if (formatString[i] == '}')
{
// A brace is either doubled (a literal brace) or unmatched. An unmatched brace
// cannot be reproduced in an interpolated string, so the fix declines.
if (i + 1 >= formatString.Length || formatString[i + 1] != '}')

AddLiteralText(formatString, literalStart, i, contents);
i = ProcessPlaceholder(formatString, i, arguments, contents);
if (i < 0)
{
return null;
}

contents.Add(CreateTextContent("}"));
literalStart = i;
}
else if (formatString[i] != '}')
{
i++;
}
else if (i + 1 < formatString.Length && formatString[i + 1] == '}')
{
i += DoubledBraceLength;
}
else
{
i = ProcessLiteralText(formatString, i, contents);
return null;
}
}

AddLiteralText(formatString, literalStart, formatString.Length, contents);

return SyntaxFactory.InterpolatedStringExpression(
SyntaxFactory.Token(SyntaxKind.InterpolatedStringStartToken),
SyntaxFactory.List(contents));
}

private static int ProcessOpenBrace(
private static void AddLiteralText(
string formatString,
int i,
ExpressionSyntax[] arguments,
int start,
int end,
List<InterpolatedStringContentSyntax> contents)
{
if (i + 1 < formatString.Length && formatString[i + 1] == '{')
if (start >= end)
{
contents.Add(CreateTextContent("{"));
return i + DoubledBraceLength;
return;
}

var closeIndex = formatString.IndexOf('}', i);
contents.Add(CreateTextContent(formatString.Substring(start, end - start)));
}

private static int ProcessPlaceholder(
string formatString,
int start,
ExpressionSyntax[] arguments,
List<InterpolatedStringContentSyntax> contents)
{
var closeIndex = formatString.IndexOf('}', start);
if (closeIndex < 0)
{
return -1;
}

var placeholder = formatString.Substring(i + 1, closeIndex - i - 1);
var placeholder = formatString.Substring(start + 1, closeIndex - start - 1);
var interpolation = BuildInterpolation(placeholder, arguments);
if (interpolation is null)
{
Expand Down Expand Up @@ -203,23 +219,7 @@ private static int ProcessOpenBrace(
return SyntaxFactory.Interpolation(arguments[argIndex]);
}

private static int ProcessLiteralText(
string formatString,
int start,
List<InterpolatedStringContentSyntax> contents)
{
var i = start;
while (i < formatString.Length && formatString[i] != '{' && formatString[i] != '}')
{
i++;
}

contents.Add(CreateTextContent(formatString.Substring(start, i - start)));
return i;
}

// The token carries source text, so a backslash, a quote, or a line break must be escaped.
// The format string holds the runtime value, and a raw value breaks the emitted literal.
// Escape source syntax in Text. Keep format-string content, including doubled braces, in ValueText.
private static InterpolatedStringTextSyntax CreateTextContent(string text)
{
var escaped = EscapeText(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,56 @@ void M()
""");
}

[Fact]
[Trait("Category", "CI")]
public Task StringFormat_EscapedBraces_StayLiteral()
{
return VerifyFixAsync(
"""
class C
{
void M(int n)
{
var s = {|E128015:string.Format("{{x}} {0}", n)|};
}
}
""",
"""
class C
{
void M(int n)
{
var s = $"{{x}} {n}";
}
}
""");
}

[Fact]
[Trait("Category", "CI")]
public Task StringFormat_EscapedBracesBesidePlaceholder_StayLiteral()
{
return VerifyFixAsync(
"""
class C
{
void M(int n)
{
var s = {|E128015:string.Format("{{{0}}} {{ {0} }}", n)|};
}
}
""",
"""
class C
{
void M(int n)
{
var s = $"{{{n}}} {{ {n} }}";
}
}
""");
}

[Fact]
[Trait("Category", "CI")]
public Task StringFormat_UnmatchedBrace_NoFixOffered()
Expand Down