mirror of
https://github.com/github/codeql.git
synced 2025-12-16 08:43:11 +01:00
Merge pull request #4 from hvitved/csharp/whitespaces
C#: Fix whitespaces
This commit is contained in:
23
csharp/fix-whitespaces.sh
Executable file
23
csharp/fix-whitespaces.sh
Executable file
@@ -0,0 +1,23 @@
|
||||
BASEDIR=$(dirname "$0")
|
||||
|
||||
cat > "$BASEDIR/reformat.vim" <<"EOF"
|
||||
:set ff=unix ts=2 et
|
||||
:retab!
|
||||
:%s/\r//g
|
||||
:%s/ \+$//
|
||||
:wq
|
||||
EOF
|
||||
|
||||
find "$BASEDIR" \( -name "*.ql" -or -name "*.qll" -or -name "*.csv" \) -exec vim -u /dev/null -s reformat.vim {} \;
|
||||
|
||||
cat > reformat.vim <<"EOF"
|
||||
:set ff=unix ts=4 et
|
||||
:retab!
|
||||
:%s/\r//g
|
||||
:%s/ \+$//
|
||||
:wq
|
||||
EOF
|
||||
|
||||
find "$BASEDIR" \( -name "*.cs" \) -exec vim -u /dev/null -s reformat.vim {} \;
|
||||
|
||||
rm "$BASEDIR/reformat.vim"
|
||||
@@ -19,7 +19,7 @@ import semmle.code.csharp.commons.StructuralComparison as SC
|
||||
/** A structural comparison configuration for comparing the conditions of nested `for` loops. */
|
||||
class NestedForConditions extends SC::StructuralComparisonConfiguration {
|
||||
NestedForConditions() { this = "Compare nested for conditions" }
|
||||
|
||||
|
||||
override predicate candidate(Element e1, Element e2) {
|
||||
exists(NestedForLoopSameVariable nested |
|
||||
e1 = nested.getInnerForStmt().getCondition() and
|
||||
|
||||
@@ -62,7 +62,7 @@ abstract class UnbracedControlStmt extends Stmt
|
||||
getSuccessorStmt() = result
|
||||
and getBlockStmt(this) = getBlockStmt(result)
|
||||
}
|
||||
|
||||
|
||||
private Location getBodyLocation() {
|
||||
result = getBody().getLocation()
|
||||
}
|
||||
|
||||
@@ -14,4 +14,4 @@ public static void foo(int count)
|
||||
}
|
||||
timesLeft--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,4 +15,4 @@ import csharp
|
||||
from Callable c
|
||||
where c.isSourceDeclaration()
|
||||
select c, c.getCyclomaticComplexity() as n
|
||||
order by n desc
|
||||
order by n desc
|
||||
|
||||
@@ -14,4 +14,4 @@ import csharp
|
||||
from Callable c
|
||||
where c.isSourceDeclaration()
|
||||
select c, c.getNumberOfLinesOfCode() as n
|
||||
order by n desc
|
||||
order by n desc
|
||||
|
||||
@@ -14,4 +14,4 @@ import csharp
|
||||
from Callable c
|
||||
where c.isSourceDeclaration()
|
||||
select c, c.getNumberOfLinesOfComments() as n
|
||||
order by n desc
|
||||
order by n desc
|
||||
|
||||
@@ -13,7 +13,7 @@ import csharp
|
||||
|
||||
from Callable c, int n
|
||||
where c.isSourceDeclaration()
|
||||
and n = count(Stmt s | s.getEnclosingCallable() = c
|
||||
and n = count(Stmt s | s.getEnclosingCallable() = c
|
||||
and s != c.getAChild()) // we do not count the top-level block
|
||||
select c, n
|
||||
order by n desc
|
||||
|
||||
@@ -13,4 +13,4 @@ import csharp
|
||||
|
||||
from Callable f, int loc
|
||||
where f.isSourceDeclaration() and loc = f.getNumberOfLines() and loc > 0
|
||||
select f, 100.0 * ((float)f.getNumberOfLinesOfComments() / (float)loc)
|
||||
select f, 100.0 * ((float)f.getNumberOfLinesOfComments() / (float)loc)
|
||||
|
||||
@@ -12,4 +12,4 @@ public void printCharacterCodes(string[] strings)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,4 +16,4 @@ public static void Main(string[] args)
|
||||
PrintAllCharInts(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,4 +18,4 @@ int f(int i, int j) {
|
||||
}
|
||||
return result;
|
||||
// end
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,14 +10,14 @@
|
||||
* @tags maintainability
|
||||
* documentation
|
||||
*/
|
||||
|
||||
|
||||
import csharp
|
||||
|
||||
from SourceFile f, int n
|
||||
where n = count(CommentLine line |
|
||||
exists(CommentBlock block |
|
||||
where n = count(CommentLine line |
|
||||
exists(CommentBlock block |
|
||||
block.getLocation().getFile() = f and
|
||||
line = block.getAProbableCodeLine())
|
||||
)
|
||||
select f, n
|
||||
order by n desc
|
||||
order by n desc
|
||||
|
||||
@@ -15,10 +15,10 @@ import external.CodeDuplication
|
||||
|
||||
from SourceFile f, int n
|
||||
where n = count(int line |
|
||||
exists(DuplicateBlock d | d.sourceFile() = f |
|
||||
line in [d.sourceStartLine()..d.sourceEndLine()] and
|
||||
not whitelistedLineForDuplication(f, line)
|
||||
)
|
||||
)
|
||||
exists(DuplicateBlock d | d.sourceFile() = f |
|
||||
line in [d.sourceStartLine()..d.sourceEndLine()] and
|
||||
not whitelistedLineForDuplication(f, line)
|
||||
)
|
||||
)
|
||||
select f, n
|
||||
order by n desc
|
||||
order by n desc
|
||||
|
||||
@@ -15,4 +15,4 @@ import semmle.code.csharp.frameworks.Test
|
||||
from SourceFile f, int n
|
||||
where n = strictcount(TestMethod test | test.fromSource() and test.getFile() = f)
|
||||
select f, n
|
||||
order by n desc
|
||||
order by n desc
|
||||
|
||||
@@ -15,16 +15,16 @@ import semmle.code.csharp.metrics.Coupling
|
||||
/* Self-containedness on file level */
|
||||
from File f, float selfContaindness, int efferentSourceCoupling, int efferentCoupling
|
||||
|
||||
where efferentSourceCoupling = count(File g|
|
||||
exists(RefType c| c.fromSource() and c.getFile() = g and
|
||||
where efferentSourceCoupling = count(File g|
|
||||
exists(RefType c| c.fromSource() and c.getFile() = g and
|
||||
exists(RefType d| d.fromSource() and d.getFile()= f and depends(d,c))
|
||||
)
|
||||
)
|
||||
and efferentCoupling = count(File g|
|
||||
exists(RefType c| c.getFile() = g and
|
||||
)
|
||||
and efferentCoupling = count(File g|
|
||||
exists(RefType c| c.getFile() = g and
|
||||
exists(RefType d| d.fromSource() and d.getFile() = f and depends(d,c))
|
||||
)
|
||||
)
|
||||
)
|
||||
and if efferentCoupling = 0 then selfContaindness = 100 else
|
||||
selfContaindness = 100*(float)efferentSourceCoupling/efferentCoupling
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import csharp
|
||||
import external.VCS
|
||||
|
||||
int committedFiles(Commit commit) {
|
||||
result = count(commit.getAnAffectedFile())
|
||||
result = count(commit.getAnAffectedFile())
|
||||
}
|
||||
|
||||
from File f
|
||||
|
||||
@@ -11,16 +11,16 @@ import csharp
|
||||
import external.VCS
|
||||
|
||||
predicate inRange(Commit first, Commit second) {
|
||||
first.getAnAffectedFile() = second.getAnAffectedFile() and
|
||||
first != second and
|
||||
exists(int n | n = first.getDate().daysTo(second.getDate()) and
|
||||
n >= 0 and n < 5)
|
||||
first.getAnAffectedFile() = second.getAnAffectedFile() and
|
||||
first != second and
|
||||
exists(int n | n = first.getDate().daysTo(second.getDate()) and
|
||||
n >= 0 and n < 5)
|
||||
}
|
||||
|
||||
int recommitsForFile(File f) {
|
||||
result = count(Commit recommit |
|
||||
f = recommit.getAnAffectedFile() and
|
||||
exists(Commit prev | inRange(prev, recommit)))
|
||||
result = count(Commit recommit |
|
||||
f = recommit.getAnAffectedFile() and
|
||||
exists(Commit prev | inRange(prev, recommit)))
|
||||
}
|
||||
|
||||
from File f
|
||||
|
||||
@@ -13,4 +13,4 @@ import external.VCS
|
||||
from File f, int n
|
||||
where n = count(Commit e | e.getAnAffectedFile() = f and e.daysToNow() <= 180 and not artificialChange(e))
|
||||
select f, n
|
||||
order by n desc
|
||||
order by n desc
|
||||
|
||||
@@ -12,4 +12,4 @@ abstract class ShortRedBook : RedBook { /* ... */ }
|
||||
abstract class LongBlueBook : BlueBook { /* ... */ }
|
||||
abstract class ShortBlueBook : BlueBook { /* ... */ }
|
||||
class ShortBlueNovel : ShortBlueBook { /* ... */ }
|
||||
// ...
|
||||
// ...
|
||||
|
||||
@@ -14,4 +14,4 @@ import csharp
|
||||
from ValueOrRefType t
|
||||
where t.isSourceDeclaration()
|
||||
select t, t.getInheritanceDepth() as n
|
||||
order by n desc
|
||||
order by n desc
|
||||
|
||||
@@ -15,4 +15,4 @@ class Book
|
||||
{
|
||||
// ...
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,4 +33,4 @@ class TLackOfCohesionCK
|
||||
var5 = 1;
|
||||
Console.WriteLine(var5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,4 +33,4 @@ class TLackOfCohesionCK
|
||||
var5 = 1;
|
||||
Console.WriteLine(var5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +22,4 @@ class HTTPRequest
|
||||
HTTPRequestResponseEndEvent(this, new HTTPRequestResponseEndArgs());
|
||||
HTTPRequestResponseProcessedEvent(this, new HTTPRequestResponseProcessedArgs());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,4 +20,4 @@ class HTTPRequest
|
||||
HTTPRequestStateChangeEvent(this, new HTTPRequestStateChangeArgs("responseend"));
|
||||
HTTPRequestStateChangeEvent(this, new HTTPRequestStateChangeArgs("responseprocessed"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,4 @@ class Person
|
||||
private string town;
|
||||
private string postcode;
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,4 @@ class Address
|
||||
private string town;
|
||||
private string postcode;
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,4 @@ class Person
|
||||
private string town;
|
||||
private string postcode;
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@ import csharp
|
||||
|
||||
from ValueOrRefType t, int n
|
||||
where
|
||||
t.isSourceDeclaration()
|
||||
and n = count(Field f |
|
||||
f.getDeclaringType() = t
|
||||
and not(f instanceof MemberConstant)
|
||||
)
|
||||
t.isSourceDeclaration()
|
||||
and n = count(Field f |
|
||||
f.getDeclaringType() = t
|
||||
and not(f instanceof MemberConstant)
|
||||
)
|
||||
select t, n
|
||||
order by n desc
|
||||
order by n desc
|
||||
|
||||
@@ -19,4 +19,4 @@ class Address
|
||||
private string town;
|
||||
private string postcode;
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,4 +44,4 @@ class Person
|
||||
}
|
||||
}
|
||||
// more properties...
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,4 +59,4 @@ class Name
|
||||
_firstName = firstUC + value.Substring(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
import csharp
|
||||
|
||||
from ValueOrRefType t, int n
|
||||
where t.isSourceDeclaration()
|
||||
and n = count(Stmt s | s.getEnclosingCallable().getDeclaringType() = t
|
||||
where t.isSourceDeclaration()
|
||||
and n = count(Stmt s | s.getEnclosingCallable().getDeclaringType() = t
|
||||
and s != s.getEnclosingCallable().getAChild()) // we do not count the top-level block
|
||||
select t, n
|
||||
order by n desc
|
||||
|
||||
@@ -6,9 +6,9 @@ public partial class UnmanagedCodeExample : Form
|
||||
{
|
||||
[DllImport("User32.dll")]
|
||||
public static extern int MessageBox(int h, string m, string c, int type); // violation
|
||||
|
||||
|
||||
private void btnSayHello_Click(object sender, EventArgs e)
|
||||
{
|
||||
MessageBox(0, "Hello World", "Title", 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,4 +7,4 @@ public partial class ManagedCodeExample : Form
|
||||
{
|
||||
MessageBox.Show("Hello World", "Title");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
using System;
|
||||
|
||||
class NestedLoopsSameVariable
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
for (int j = 0; j < 2; i++)
|
||||
{
|
||||
Console.WriteLine(i + " " + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
|
||||
class NestedLoopsSameVariable
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
for (int j = 0; j < 2; i++)
|
||||
{
|
||||
Console.WriteLine(i + " " + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,100 +1,100 @@
|
||||
using System;
|
||||
|
||||
class Test
|
||||
{
|
||||
void SameCondition()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
// GOOD: Same condition
|
||||
for (; i<10; ++i)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DifferentCondition1()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
// BAD: considered to be a different condition
|
||||
for (; 10>i; ++i)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DifferentCondition2()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
// BAD: different condition
|
||||
for (; i<9; ++i)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DifferentConditions3()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
// BAD: different condition
|
||||
for (; i<=10; ++i)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UseAfterInnerLoop()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
for (; i<10; ++i)
|
||||
{
|
||||
}
|
||||
|
||||
// BAD: i is not guarded
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
|
||||
void GuardedAfterInnerLoop()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
|
||||
// GOOD: same condition
|
||||
for (; i<10; ++i)
|
||||
{
|
||||
}
|
||||
|
||||
if (10 > i)
|
||||
{
|
||||
// GOOD: i is guarded
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GuardedByContinue()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
|
||||
// GOOD: same condition
|
||||
for (; i<10; ++i)
|
||||
{
|
||||
}
|
||||
|
||||
if (10 <= i) continue;
|
||||
|
||||
// GOOD: i is guarded
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
|
||||
class Test
|
||||
{
|
||||
void SameCondition()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
// GOOD: Same condition
|
||||
for (; i<10; ++i)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DifferentCondition1()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
// BAD: considered to be a different condition
|
||||
for (; 10>i; ++i)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DifferentCondition2()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
// BAD: different condition
|
||||
for (; i<9; ++i)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DifferentConditions3()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
// BAD: different condition
|
||||
for (; i<=10; ++i)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UseAfterInnerLoop()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
for (; i<10; ++i)
|
||||
{
|
||||
}
|
||||
|
||||
// BAD: i is not guarded
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
|
||||
void GuardedAfterInnerLoop()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
|
||||
// GOOD: same condition
|
||||
for (; i<10; ++i)
|
||||
{
|
||||
}
|
||||
|
||||
if (10 > i)
|
||||
{
|
||||
// GOOD: i is guarded
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GuardedByContinue()
|
||||
{
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
|
||||
// GOOD: same condition
|
||||
for (; i<10; ++i)
|
||||
{
|
||||
}
|
||||
|
||||
if (10 <= i) continue;
|
||||
|
||||
// GOOD: i is guarded
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user