Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions csharp/ql/src/codeql-suites/csharp-code-quality.qls
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
- cs/constant-condition
- cs/useless-gethashcode-call
- cs/non-short-circuit
- cs/useless-assignment-to-local
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public class DeadStoreOfLocal

public int M1()
{
int x = M2(); // BAD
int x = M2(); // $ Alert
return (x = 1) + x; // GOOD
}

public int M2()
{
int x = 1; // GOOD
return x + (x = 1); // BAD
return x + (x = 1); // $ Alert
}

public int M3()
Expand All @@ -41,19 +41,19 @@ public int M4()

public void M5()
{
int x = M3(); // BAD
int x = M3(); // $ Alert
}

public void M6()
{
int x = 42;
x += 1; // BAD
x += 1; // $ Alert
}

public void M7()
{
int x = 42;
x++; // BAD
x++; // $ Alert
}

public IEnumerable<string> M8(IEnumerable<string> source)
Expand All @@ -79,8 +79,8 @@ public IEnumerable<string> M9(IEnumerable<string> source)

public void M10(IEnumerable<string> source)
{
foreach (var val in source)
{ // BAD
foreach (var val in source) // $ Alert
{
}
}
}
Expand All @@ -98,10 +98,10 @@ public void F()
message = "Unsuccessful completion"; // GOOD: Used in finally
Process();
info2 = "Finishing"; // GOOD: Used in exception handler
extra = "Dead store here"; // BAD: Dead store
extra = "Dead store here"; // $ Alert Dead store
Process();
message = "Successful completion"; // GOOD: Used in finally
info1 = "Used in handler"; // BAD: Used in handler, but not a reachable handler
info1 = "Used in handler"; // $ Alert Used in handler, but not a reachable handler
}
catch (SystemException ex)
{
Expand Down Expand Up @@ -139,7 +139,7 @@ public void FinallyFlow2()
{
Process();
}
catch (Exception ex) // BAD
catch (Exception ex) // $ Alert
{
Console.WriteLine("Stage " + stage);
stage = 3; // GOOD: Used in finally
Expand All @@ -157,7 +157,7 @@ public class OutParam
public void Test()
{
int x;
Fn(out x); // BAD
Fn(out x); // Missing Alert
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use $ MISSING: Alert instead

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the same in other places.

Fn(out _); // GOOD
}

Expand Down Expand Up @@ -194,7 +194,7 @@ void M1()

void M2()
{
var x = M6(); // BAD [FALSE NEGATIVE]
var x = M6(); // Missing Alert
Action a = () =>
{
x = 1; // GOOD
Expand All @@ -208,7 +208,7 @@ void M3()
int x;
Action a = () =>
{
x = 1; // BAD [FALSE NEGATIVE]
x = 1; // Missing Alert
};
a();
}
Expand All @@ -230,7 +230,7 @@ void M4()

void M5()
{
int x = 0; // BAD: NOT DETECTED
int x = 0; // Missing Alert.
Action a = () =>
{
x = 1; // GOOD
Expand All @@ -243,22 +243,22 @@ int M6()
{
fn(() =>
{
int y = M6(); // BAD
int y = M6(); // $ Alert
return (y = 1) + y; // GOOD
});

int captured = 0; // GOOD: Variable captured variable
fn(() => { return captured; });

return captured = 1; // BAD: NOT DETECTED
return captured = 1; // Missing Alert.
}

void M7()
{
var y = 12; // GOOD: Not a dead store (used in delegate)
fn(() =>
{
var x = y; // BAD: Dead store in lambda
var x = y; // $ Alert Dead store in lambda
return 0;
});
}
Expand Down Expand Up @@ -297,8 +297,8 @@ void Test()
{ // GOOD
Console.WriteLine($"int {i1}");
}
else if (o is var v1)
{ // BAD
else if (o is var v1) // $ Alert
{
}

switch (o)
Expand All @@ -311,7 +311,7 @@ void Test()
case int i3: // GOOD
Console.WriteLine($"int {i3}");
break;
case var v2: // BAD
case var v2: // $ Alert
break;
default:
Console.WriteLine("Something else");
Expand All @@ -328,7 +328,7 @@ void M()
Use(x);
Use(b);
Use(s);
(x, (b, s)) = GetTuple(); // BAD: `b`
(x, (b, s)) = GetTuple(); // $ Alert on `b`
Use(x);
Use(s);
(x, (_, s)) = GetTuple(); // GOOD
Expand Down Expand Up @@ -369,7 +369,7 @@ string M3()

string M4()
{
var s = M3(); // BAD
var s = M3(); // $ Alert
s = "";
return s;
}
Expand All @@ -395,7 +395,7 @@ string M7(bool b)
{
var s = "";
if (b)
s = "abc"; // BAD
s = "abc"; // $ Alert
if (!b)
return s;
return null;
Expand Down Expand Up @@ -469,8 +469,18 @@ public static void M()
using var x = new System.IO.FileStream("", System.IO.FileMode.Open); // GOOD
using var _ = new System.IO.FileStream("", System.IO.FileMode.Open); // GOOD

using (var y = new System.IO.FileStream("", System.IO.FileMode.Open)) // BAD
using (var y = new System.IO.FileStream("", System.IO.FileMode.Open)) // $ Alert
{
}
}
}
}

class StringInterpolation
{
void Pi()
{
float pi = 3.14159f; // GOOD
const int align = 6; // GOOD
Console.WriteLine($"Pi, {pi,align:F3}");
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Dead Code/DeadStoreOfLocal.ql
query: Dead Code/DeadStoreOfLocal.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Bad
{
double ParseInt(string s)
{
var success = int.TryParse(s, out int i);
var success = int.TryParse(s, out int i); // $ Alert
return i;
}

Expand All @@ -20,7 +20,7 @@ double ParseDouble(string s)
{
return double.Parse(s);
}
catch (FormatException e)
catch (FormatException e) // $ Alert
{
return double.NaN;
}
Expand All @@ -29,14 +29,14 @@ double ParseDouble(string s)
int Count(string[] ss)
{
int count = 0;
foreach (var s in ss)
foreach (var s in ss) // $ Alert
count++;
return count;
}

string IsInt(object o)
{
if (o is int i)
if (o is int i) // $ Alert
return "yes";
else
return "no";
Expand All @@ -46,7 +46,7 @@ string IsString(object o)
{
switch (o)
{
case string s:
case string s: // $ Alert
return "yes";
default:
return "no";
Expand Down