Skip to content

Commit

Permalink
Fix formatter removing braces on unused local variable declarations (…
Browse files Browse the repository at this point in the history
…which shouldn't exist anyway, decompiler output garbage)
  • Loading branch information
Chicken-Bones committed Feb 3, 2020
1 parent 1a15654 commit ad6cb83
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
24 changes: 9 additions & 15 deletions patches/merged/Terraria/Main.cs.patch
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,18 @@
}
else if (SocialAPI.Cloud != null) {
SocialAPI.Cloud.Delete(WorldList[i].Path);
@@ -4723,15 +_,10 @@

public void DedServ() {
@@ -4725,9 +_,11 @@
rand = new UnifiedRandom();
- if (autoShutdown) {
+ if (autoShutdown)
if (autoShutdown) {
string lpWindowName = Console.Title = "terraria" + rand.Next(int.MaxValue);
- IntPtr intPtr = FindWindow(null, lpWindowName);
- if (intPtr != IntPtr.Zero)
- ShowWindow(intPtr, 0);
- }
- else {
+ else
+#if WINDOWS
IntPtr intPtr = FindWindow(null, lpWindowName);
if (intPtr != IntPtr.Zero)
ShowWindow(intPtr, 0);
+#endif
}
else {
Console.Title = "Terraria Server " + versionNumber2;
- }

dedServ = true;
showSplash = false;
@@ -5439,13 +_,17 @@

public Main() {
Expand Down
4 changes: 2 additions & 2 deletions patches/merged/Terraria/NPC.cs.patch
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
--- src/decompiled\Terraria\NPC.cs
+++ src/merged\Terraria\NPC.cs
@@ -24184,6 +_,7 @@
@@ -24188,6 +_,7 @@
float num1171 = 1f + Main.rand.NextFloat();
float fadeIn = 0.4f + Main.rand.NextFloat();
int num1172 = Utils.SelectRandom<int>(Main.rand, 31, 229);
+#if CLIENT
if (flag76) {
MoonlordDeathDrama.AddExplosion(vector146);
for (float num1173 = 0f; num1173 < num1168 * 2f; num1173++) {
@@ -24195,6 +_,8 @@
@@ -24199,6 +_,8 @@
dust2.scale = num1171;
}
}
Expand Down
15 changes: 8 additions & 7 deletions setup/Setup/FormatTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override SyntaxNode VisitIfStatement(IfStatementSyntax node) {

public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax method) {
if (method.Body != null &&
IsSingleLineStatement(method.Body) &&
CanStripSurroundingBraces(method.Body) &&
method.Body.DescendantTrivia().All(IsWhitespaceTrivia) &&
method.Body.Statements[0] is ReturnStatementSyntax returnStatement &&
returnStatement.Expression != null) {
Expand All @@ -92,7 +92,7 @@ private static bool NoClausesNeedBraces(IfStatementSyntax ifStmt) {

// lets not destroy the stack
while (true) {
if (!IsSingleLineStatement(ifStmt.Statement))
if (!CanStripSurroundingBraces(ifStmt.Statement))
return false;

var elseStmt = ifStmt.Else?.Statement;
Expand All @@ -102,19 +102,20 @@ private static bool NoClausesNeedBraces(IfStatementSyntax ifStmt) {
if (elseStmt is IfStatementSyntax elseifStmt)
ifStmt = elseifStmt;
else
return IsSingleLineStatement(elseStmt);
return CanStripSurroundingBraces(elseStmt);
}
}

private static bool IsSingleLineStatement(StatementSyntax node) {
private static bool CanStripSurroundingBraces(StatementSyntax node) {
switch (node) {
case BlockSyntax block:
return block.Statements.Count == 1 &&
block.GetLeadingTrivia().All(IsWhitespaceTrivia) &&
block.GetTrailingTrivia().All(IsWhitespaceTrivia) &&
IsSingleLineStatement(block.Statements[0]);
case IfStatementSyntax _:
return false; // removing braces around if statements can change semantics
CanStripSurroundingBraces(block.Statements[0]);
case IfStatementSyntax _: // removing braces around if statements can change semantics
case LocalDeclarationStatementSyntax _: // removing braces around variable declarations is invalid
return false;
default:
return !SpansMultipleLines(node);
}
Expand Down

0 comments on commit ad6cb83

Please sign in to comment.