Skip to content

Commit 2bc998b

Browse files
authored
Improve RCS0056 (#1373)
1 parent 301a51a commit 2bc998b

File tree

3 files changed

+174
-12
lines changed

3 files changed

+174
-12
lines changed

ChangeLog.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424

2525
- Fix analyzer [RCS1055](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1055) ([PR](https://github.com/dotnet/roslynator/pull/1361))
2626
- Fix analyzer [RCS1261](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1261) ([PR](https://github.com/dotnet/roslynator/pull/1374))
27+
- Fix analyzer [RCS0056](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0056) ([PR](https://github.com/dotnet/roslynator/pull/1373))
2728

2829
## [4.9.0] - 2024-01-10
2930

src/Formatting.Analyzers/CSharp/LineIsTooLongAnalyzer.cs

+61-8
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,76 @@ private static void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
8282
if (line.Span.Length <= maxLength)
8383
continue;
8484

85+
int start = line.Start;
8586
int end = line.End;
8687

8788
SyntaxToken token = root.FindToken(end);
8889

89-
if (token.IsKind(SyntaxKind.StringLiteralToken))
90+
if (token.IsKind(SyntaxKind.None)
91+
|| token.Span.End < start)
9092
{
91-
TextSpan span = token.Span;
93+
continue;
94+
}
95+
96+
SyntaxToken token2 = token;
9297

93-
if (span.End == end)
98+
if (token2.IsKind(SyntaxKind.CommaToken, SyntaxKind.SemicolonToken))
99+
token2 = token2.GetPreviousToken();
100+
101+
if (!token2.IsKind(SyntaxKind.None)
102+
&& token2.Span.End >= start)
103+
{
104+
while (token2.IsKind(
105+
SyntaxKind.CloseParenToken,
106+
SyntaxKind.CloseBraceToken,
107+
SyntaxKind.CloseBracketToken))
94108
{
95-
if (span.Length >= maxLength)
96-
continue;
109+
token2 = token2.GetPreviousToken();
97110
}
98-
else if (span.Contains(end)
99-
&& end - span.Start >= maxLength)
111+
112+
if (token2.IsKind(
113+
SyntaxKind.StringLiteralToken,
114+
#if ROSLYN_4_2
115+
SyntaxKind.InterpolatedRawStringEndToken,
116+
#endif
117+
SyntaxKind.InterpolatedStringEndToken))
100118
{
101-
continue;
119+
SyntaxNode parent = token2.Parent;
120+
121+
if (parent.SpanStart <= start)
122+
continue;
123+
124+
token2 = parent.GetFirstToken().GetPreviousToken();
125+
126+
if (token2.IsKind(SyntaxKind.None)
127+
|| token2.Span.End < start)
128+
{
129+
continue;
130+
}
131+
132+
if (parent.IsParentKind(
133+
SyntaxKind.ArrowExpressionClause,
134+
SyntaxKind.Argument,
135+
SyntaxKind.AttributeArgument))
136+
{
137+
SyntaxToken firstToken = parent.Parent.GetFirstToken();
138+
139+
if (firstToken.SpanStart >= start)
140+
{
141+
SyntaxToken token3 = firstToken.GetPreviousToken();
142+
143+
if (token3.IsKind(SyntaxKind.None)
144+
|| token3.Span.End < start)
145+
{
146+
continue;
147+
}
148+
}
149+
}
150+
151+
if (parent.Span.End > end)
152+
{
153+
i = lines.IndexOf(parent.Span.End) - 1;
154+
}
102155
}
103156
}
104157

src/Tests/Formatting.Analyzers.Tests/RCS0056LineIsTooLongTests.cs

+112-4
Original file line numberDiff line numberDiff line change
@@ -1000,24 +1000,24 @@ class C
10001000
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
10011001
public async Task TestNoFix_ExpressionBody_AlreadyWrapped()
10021002
{
1003-
await VerifyDiagnosticAndNoFixAsync(
1003+
await VerifyNoDiagnosticAsync(
10041004
@"
10051005
class C
10061006
{
10071007
string M(object p)
1008-
[| => ""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"";|]
1008+
=> ""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"";
10091009
}
10101010
");
10111011
}
10121012

10131013
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
10141014
public async Task TestNoFix_ExpressionBody_AlreadyWrapped2()
10151015
{
1016-
await VerifyDiagnosticAndNoFixAsync(@"
1016+
await VerifyNoDiagnosticAsync(@"
10171017
class C
10181018
{
10191019
string M(object p) =>
1020-
[| ""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"";|]
1020+
""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"";
10211021
}
10221022
");
10231023
}
@@ -1180,4 +1180,112 @@ void M()
11801180
}
11811181
");
11821182
}
1183+
1184+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
1185+
public async Task TestNoDiagnostic_LongStringLiteral_Argument()
1186+
{
1187+
await VerifyNoDiagnosticAsync("""
1188+
class C
1189+
{
1190+
static void M(string x, string y)
1191+
{
1192+
C.M(
1193+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
1194+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
1195+
}
1196+
}
1197+
""");
1198+
}
1199+
1200+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
1201+
public async Task TestNoDiagnostic_LongStringLiteral_AttributeArgument()
1202+
{
1203+
await VerifyNoDiagnosticAsync("""
1204+
using System;
1205+
1206+
class C
1207+
{
1208+
[Obsolete(
1209+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")]
1210+
static void M()
1211+
{
1212+
}
1213+
}
1214+
""");
1215+
}
1216+
1217+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
1218+
public async Task TestNoDiagnostic_LongStringLiteral_AttributeArgument2()
1219+
{
1220+
await VerifyNoDiagnosticAsync("""
1221+
using System;
1222+
1223+
class C
1224+
{
1225+
[My(
1226+
Value = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")]
1227+
static void M()
1228+
{
1229+
}
1230+
}
1231+
1232+
class MyAttribute : Attribute
1233+
{
1234+
public string Value { get; set; }
1235+
}
1236+
""");
1237+
}
1238+
1239+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
1240+
public async Task TestNoDiagnostic_LongVerbatimStringLiteral()
1241+
{
1242+
await VerifyNoDiagnosticAsync("""
1243+
class C
1244+
{
1245+
static void M(string x, int y)
1246+
{
1247+
C.M(
1248+
@"
1249+
1250+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
1251+
0);
1252+
}
1253+
}
1254+
""");
1255+
}
1256+
1257+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
1258+
public async Task TestNoDiagnostic_LongInterpolatedString()
1259+
{
1260+
await VerifyNoDiagnosticAsync("""
1261+
class C
1262+
{
1263+
static void M(string x, string y)
1264+
{
1265+
C.M(
1266+
$"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
1267+
$"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
1268+
}
1269+
}
1270+
""");
1271+
}
1272+
1273+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.LineIsTooLong)]
1274+
public async Task TestNoDiagnostic_LongRawInterpolatedString()
1275+
{
1276+
await VerifyNoDiagnosticAsync(""""
1277+
class C
1278+
{
1279+
static void M(string x, int y)
1280+
{
1281+
C.M(
1282+
$"""
1283+
1284+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1285+
""",
1286+
0);
1287+
}
1288+
}
1289+
"""");
1290+
}
11831291
}

0 commit comments

Comments
 (0)