Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c38bf5e

Browse files
committed
C#: Reduce nesting and fix some formatting
1 parent e73ced2 commit c38bf5e

11 files changed

Lines changed: 122 additions & 108 deletions

File tree

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ bool IBuildActions.FileExists(string file)
3535
FileExistsIn.Add(file);
3636
if (FileExists.TryGetValue(file, out var ret))
3737
return ret;
38+
3839
if (FileExists.TryGetValue(Path.GetFileName(file), out ret))
3940
return ret;
41+
4042
throw new ArgumentException("Missing FileExists " + file);
4143
}
4244

@@ -51,28 +53,36 @@ int IBuildActions.RunProcess(string cmd, string args, string? workingDirectory,
5153
{
5254
var pattern = cmd + " " + args;
5355
RunProcessIn.Add(pattern);
54-
if (RunProcessOut.TryGetValue(pattern, out var str))
55-
stdOut = str.Split("\n");
56-
else
56+
57+
if (!RunProcessOut.TryGetValue(pattern, out var str))
5758
throw new ArgumentException("Missing RunProcessOut " + pattern);
59+
60+
stdOut = str.Split("\n");
61+
5862
RunProcessWorkingDirectory.TryGetValue(pattern, out var wd);
63+
5964
if (wd != workingDirectory)
6065
throw new ArgumentException("Missing RunProcessWorkingDirectory " + pattern);
61-
if (RunProcess.TryGetValue(pattern, out var ret))
62-
return ret;
63-
throw new ArgumentException("Missing RunProcess " + pattern);
66+
67+
if (!RunProcess.TryGetValue(pattern, out var ret))
68+
throw new ArgumentException("Missing RunProcess " + pattern);
69+
70+
return ret;
6471
}
6572

6673
int IBuildActions.RunProcess(string cmd, string args, string? workingDirectory, IDictionary<string, string>? env)
6774
{
6875
var pattern = cmd + " " + args;
6976
RunProcessIn.Add(pattern);
7077
RunProcessWorkingDirectory.TryGetValue(pattern, out var wd);
78+
7179
if (wd != workingDirectory)
7280
throw new ArgumentException("Missing RunProcessWorkingDirectory " + pattern);
73-
if (RunProcess.TryGetValue(pattern, out var ret))
74-
return ret;
75-
throw new ArgumentException("Missing RunProcess " + pattern);
81+
82+
if (!RunProcess.TryGetValue(pattern, out var ret))
83+
throw new ArgumentException("Missing RunProcess " + pattern);
84+
85+
return ret;
7686
}
7787

7888
public readonly IList<string> DirectoryDeleteIn = new List<string>();
@@ -86,18 +96,20 @@ void IBuildActions.DirectoryDelete(string dir, bool recursive)
8696

8797
bool IBuildActions.DirectoryExists(string dir)
8898
{
89-
if (DirectoryExists.TryGetValue(dir, out var ret))
90-
return ret;
91-
throw new ArgumentException("Missing DirectoryExists " + dir);
99+
if (!DirectoryExists.TryGetValue(dir, out var ret))
100+
throw new ArgumentException("Missing DirectoryExists " + dir);
101+
102+
return ret;
92103
}
93104

94105
public readonly IDictionary<string, string?> GetEnvironmentVariable = new Dictionary<string, string?>();
95106

96107
string? IBuildActions.GetEnvironmentVariable(string name)
97108
{
98-
if (GetEnvironmentVariable.TryGetValue(name, out var ret))
99-
return ret;
100-
throw new ArgumentException("Missing GetEnvironmentVariable " + name);
109+
if (!GetEnvironmentVariable.TryGetValue(name, out var ret))
110+
throw new ArgumentException("Missing GetEnvironmentVariable " + name);
111+
112+
return ret;
101113
}
102114

103115
public string GetCurrentDirectory = "";
@@ -111,18 +123,22 @@ string IBuildActions.GetCurrentDirectory()
111123

112124
IEnumerable<string> IBuildActions.EnumerateFiles(string dir)
113125
{
114-
if (EnumerateFiles.TryGetValue(dir, out var str))
115-
return str.Split("\n").Select(p => PathCombine(dir, p));
116-
throw new ArgumentException("Missing EnumerateFiles " + dir);
126+
if (!EnumerateFiles.TryGetValue(dir, out var str))
127+
throw new ArgumentException("Missing EnumerateFiles " + dir);
128+
129+
return str.Split("\n").Select(p => PathCombine(dir, p));
117130
}
118131

119132
public readonly IDictionary<string, string> EnumerateDirectories = new Dictionary<string, string>();
120133

121134
IEnumerable<string> IBuildActions.EnumerateDirectories(string dir)
122135
{
123-
if (EnumerateDirectories.TryGetValue(dir, out var str))
124-
return string.IsNullOrEmpty(str) ? Enumerable.Empty<string>() : str.Split("\n").Select(p => PathCombine(dir, p));
125-
throw new ArgumentException("Missing EnumerateDirectories " + dir);
136+
if (!EnumerateDirectories.TryGetValue(dir, out var str))
137+
throw new ArgumentException("Missing EnumerateDirectories " + dir);
138+
139+
return string.IsNullOrEmpty(str)
140+
? Enumerable.Empty<string>()
141+
: str.Split("\n").Select(p => PathCombine(dir, p));
126142
}
127143

128144
public bool IsWindows;
@@ -152,15 +168,16 @@ void IBuildActions.WriteAllText(string filename, string contents)
152168

153169
XmlDocument IBuildActions.LoadXml(string filename)
154170
{
155-
if (LoadXml.TryGetValue(filename, out var xml))
156-
return xml;
157-
throw new ArgumentException("Missing LoadXml " + filename);
171+
if (!LoadXml.TryGetValue(filename, out var xml))
172+
throw new ArgumentException("Missing LoadXml " + filename);
173+
return xml;
158174
}
159175

160176
public string EnvironmentExpandEnvironmentVariables(string s)
161177
{
162178
foreach (var kvp in GetEnvironmentVariable)
163179
s = s.Replace($"%{kvp.Key}%", kvp.Value);
180+
164181
return s;
165182
}
166183

csharp/autobuilder/Semmle.Autobuild.Shared/CommandBuilder.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ void ArgvQuote(string argument, bool force)
9999
}
100100
else
101101
{
102-
if (cmd) arguments.Append('^');
102+
if (cmd)
103+
arguments.Append('^');
104+
103105
arguments.Append('\"');
106+
104107
for (int it = 0; ; ++it)
105108
{
106109
var numBackslashes = 0;
@@ -115,10 +118,12 @@ void ArgvQuote(string argument, bool force)
115118
arguments.Append('\\', numBackslashes * 2);
116119
break;
117120
}
118-
else if (argument[it] == '\"')
121+
122+
if (argument[it] == '\"')
119123
{
120124
arguments.Append('\\', numBackslashes * 2 + 1);
121-
if (cmd) arguments.Append('^');
125+
if (cmd)
126+
arguments.Append('^');
122127
arguments.Append(arguments[it]);
123128
}
124129
else
@@ -130,7 +135,10 @@ void ArgvQuote(string argument, bool force)
130135
arguments.Append(argument[it]);
131136
}
132137
}
133-
if (cmd) arguments.Append('^');
138+
139+
if (cmd)
140+
arguments.Append('^');
141+
134142
arguments.Append('\"');
135143
}
136144
}

csharp/extractor/Semmle.Extraction.CIL/Entities/Type.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,9 @@ public bool TryGetPrimitiveType([NotNullWhen(true)] out PrimitiveType? t)
238238
t = cx.Create(code);
239239
return true;
240240
}
241-
else
242-
{
243-
t = null;
244-
return false;
245-
}
241+
242+
t = null;
243+
return false;
246244
}
247245

248246
private bool TryGetPrimitiveTypeCode(out PrimitiveTypeCode code)

csharp/extractor/Semmle.Extraction.CIL/PDB/NativePdbReader.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ public Document(ISymUnmanagedDocument doc)
3030
var rawContents = document.GetEmbeddedSource().ToArray();
3131
return System.Text.Encoding.Default.GetString(rawContents);
3232
}
33-
else
34-
{
35-
return File.Exists(Path) ? File.ReadAllText(Path) : null;
36-
}
33+
34+
return File.Exists(Path)
35+
? File.ReadAllText(Path)
36+
: null;
37+
3738
});
3839
}
3940

csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,12 @@ static string GetOutputName(CSharpCompilation compilation,
173173
var entryPointFile = compilation.SyntaxTrees.First().FilePath;
174174
return Path.ChangeExtension(entryPointFile, ".exe");
175175
}
176-
else
177-
{
178-
var entryPointFilename = entry.Locations.First().SourceTree.FilePath;
179-
return Path.ChangeExtension(entryPointFilename, ".exe");
180-
}
181-
}
182-
else
183-
{
184-
return Path.Combine(commandLineArguments.OutputDirectory, commandLineArguments.OutputFileName);
176+
177+
var entryPointFilename = entry.Locations.First().SourceTree.FilePath;
178+
return Path.ChangeExtension(entryPointFilename, ".exe");
185179
}
180+
181+
return Path.Combine(commandLineArguments.OutputDirectory, commandLineArguments.OutputFileName);
186182
}
187183

188184
/// <summary>

csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,13 @@ public override Microsoft.CodeAnalysis.Location ReportingLocation
131131
{
132132
return syn.Identifier.GetLocation();
133133
}
134-
else if (symbol.IsImplicitlyDeclared)
134+
135+
if (symbol.IsImplicitlyDeclared)
135136
{
136137
return ContainingType.ReportingLocation;
137138
}
138-
else
139-
{
140-
return symbol.ContainingType.Locations.FirstOrDefault();
141-
}
139+
140+
return symbol.ContainingType.Locations.FirstOrDefault();
142141
}
143142
}
144143

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/IsPattern.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ public static Expression CreatePattern(this Context cx, PatternSyntax syntax, IE
5050

5151
return VariableDeclaration.Create(cx, symbol, type, null, cx.Create(syntax.GetLocation()), true, parent, child);
5252
}
53-
else
54-
{
55-
throw new InternalError(varPattern, "Unable to get the declared symbol of the var pattern designation.");
56-
}
53+
54+
throw new InternalError(varPattern, "Unable to get the declared symbol of the var pattern designation.");
5755
default:
5856
throw new InternalError("var pattern designation is unhandled");
5957
}

csharp/extractor/Semmle.Extraction.CSharp/Populators/Locations.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ public static Location ExtendLocation(this Location l1, SyntaxNode n2)
1919
{
2020
return l1;
2121
}
22-
else
23-
{
24-
var l2 = n2.FixedLocation();
25-
int start = System.Math.Min(l1.SourceSpan.Start, l2.SourceSpan.Start);
26-
int end = System.Math.Max(l1.SourceSpan.End, l2.SourceSpan.End);
27-
return Location.Create(n2.SyntaxTree, new Microsoft.CodeAnalysis.Text.TextSpan(start, end - start));
28-
}
22+
23+
var l2 = n2.FixedLocation();
24+
int start = System.Math.Min(l1.SourceSpan.Start, l2.SourceSpan.Start);
25+
int end = System.Math.Max(l1.SourceSpan.End, l2.SourceSpan.End);
26+
return Location.Create(n2.SyntaxTree, new Microsoft.CodeAnalysis.Text.TextSpan(start, end - start));
2927
}
3028

3129
/// <summary>

csharp/extractor/Semmle.Extraction/Context.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,9 @@ public bool ExtractGenerics(ICachedEntity entity)
149149
{
150150
return false;
151151
}
152-
else
153-
{
154-
extractedGenerics.Add(entity.Label);
155-
return true;
156-
}
152+
153+
extractedGenerics.Add(entity.Label);
154+
return true;
157155
}
158156

159157
/// <summary>

csharp/extractor/Semmle.Extraction/TrapExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static TextWriter WriteColumn(this TextWriter trapFile, object o)
9494

9595
private static bool NeedsTruncation(string s)
9696
{
97-
// Optimization: only count the actual number of bytes if there is the possibility
97+
// Optimization: only count the actual number of bytes if there is the possibility
9898
// of the string exceeding maxStringBytes
9999
return encoding.GetMaxByteCount(s.Length) > maxStringBytes &&
100100
encoding.GetByteCount(s) > maxStringBytes;
@@ -249,7 +249,10 @@ public static TextWriter BuildList<T>(this TextWriter trapFile, string separator
249249
bool first = true;
250250
foreach (var item in items)
251251
{
252-
if (first) first = false; else trapFile.Write(separator);
252+
if (first)
253+
first = false;
254+
else
255+
trapFile.Write(separator);
253256
action(item, trapFile);
254257
}
255258
return trapFile;

0 commit comments

Comments
 (0)