Skip to content

Commit 4235131

Browse files
committed
C#: Address review comments. Introduce Member::isEffectivelyPublic() because isEffectivelyPrivate and isEffectivelyInternal are almost always used together.
1 parent 26365c8 commit 4235131

File tree

10 files changed

+40
-12
lines changed

10 files changed

+40
-12
lines changed

csharp/ql/src/Language Abuse/MissedReadonlyOpportunity.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ where
3636
canBeReadonly(f) and
3737
not f.isConst() and
3838
not f.isReadOnly() and
39-
(f.isEffectivelyPrivate() or f.isEffectivelyInternal())
39+
not f.isEffectivelyPublic()
4040
select f, "Field '" + f.getName() + "' can be 'readonly'."

csharp/ql/src/Likely Bugs/Collections/WriteOnlyContainer.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ where
1818
v.getType() instanceof CollectionType and
1919
(
2020
v instanceof LocalVariable or
21-
v = any(Field f | f.isEffectivelyPrivate() or f.isEffectivelyInternal())
21+
v = any(Field f | not f.isEffectivelyPublic())
2222
) and
2323
forex(Access a | a = v.getAnAccess() |
2424
a = any(ModifierMethodCall m).getQualifier() or

csharp/ql/src/Useless code/DefaultToString.ql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ predicate alwaysDefaultToString(ValueOrRefType t) {
5555
overriding.getAMethod() instanceof ToStringMethod and
5656
overriding.getABaseType+() = t
5757
) and
58-
not t.isAbstract() and
59-
not t instanceof Interface
58+
(
59+
(t.isAbstract() or t instanceof Interface)
60+
implies
61+
not t.isEffectivelyPublic()
62+
)
6063
}
6164

6265
newtype TDefaultToStringType = TDefaultToStringType0(ValueOrRefType t) { alwaysDefaultToString(t) }

csharp/ql/src/semmle/code/csharp/Member.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ class Modifiable extends Declaration, @modifiable {
107107
this.isInternal() or
108108
this.getDeclaringType+().isInternal()
109109
}
110+
111+
/**
112+
* Holds if this declaration is effectively `public`, because it
113+
* and all enclosing types are `public`.
114+
*/
115+
predicate isEffectivelyPublic() {
116+
not isEffectivelyPrivate() and not isEffectivelyInternal()
117+
}
110118
}
111119

112120
/** A declaration that is a member of a type. */

csharp/ql/src/semmle/code/csharp/dataflow/internal/Steps.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ module Steps {
4848
* or because one of `m`'s enclosing types is).
4949
*/
5050
private predicate isEffectivelyInternalOrPrivate(Modifiable m) {
51-
m.isEffectivelyInternal() or
52-
m.isEffectivelyPrivate()
51+
not m.isEffectivelyPublic()
5352
}
5453

5554
private predicate flowIn(Parameter p, Expr pred, AssignableRead succ) {

csharp/ql/src/semmle/code/csharp/dispatch/Dispatch.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private module Internal {
294294
|
295295
succ.(AssignableRead) = a.getAnAccess() and
296296
pred = a.getAnAssignedValue() and
297-
a = any(Modifiable m | m.isEffectivelyInternal() or m.isEffectivelyPrivate())
297+
a = any(Modifiable m | not m.isEffectivelyPublic())
298298
)
299299
}
300300

csharp/ql/test/library-tests/modifiers/Effectively.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
| Modifiers.cs:41:20:41:21 | F1 | internal |
1616
| Modifiers.cs:43:26:43:27 | F2 | internal |
1717
| Modifiers.cs:47:30:47:31 | F4 | internal |
18+
| Modifiers.cs:52:19:52:19 | S | public |
19+
| Modifiers.cs:54:20:54:21 | P1 | public |
20+
| Modifiers.cs:54:36:54:38 | get_P1 | public |
21+
| Modifiers.cs:54:52:54:54 | set_P1 | public |
22+
| Modifiers.cs:55:20:55:21 | P2 | public |
23+
| Modifiers.cs:55:36:55:38 | get_P2 | public |

csharp/ql/test/library-tests/modifiers/Effectively.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ where
77
m.isEffectivelyInternal() and not m.isInternal() and s = "internal"
88
or
99
m.isEffectivelyPrivate() and not m.isPrivate() and s = "private"
10+
or
11+
m.isEffectivelyPublic() and s = "public"
1012
)
1113
select m, s

csharp/ql/test/query-tests/Useless Code/DefaultToString/DefaultToString.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Text;
33

4-
class DefaultToString
4+
public class DefaultToString
55
{
66
void M()
77
{
@@ -32,6 +32,11 @@ void M()
3232

3333
IInterface f = null;
3434
Console.WriteLine(f); // GOOD
35+
IPrivate f = null;
36+
Console.WriteLine(f); // BAD
37+
38+
IPublic g = null;
39+
Console.WriteLine(g); // GOOD
3540
}
3641

3742
class A
@@ -51,8 +56,12 @@ class D : C
5156
{
5257
override public string ToString() { return "D"; }
5358
}
54-
55-
interface IInterface
59+
60+
public interface IPublic
61+
{
62+
}
63+
64+
private interface IPrivate
5665
{
5766
}
5867
}

csharp/ql/test/query-tests/Useless Code/DefaultToString/DefaultToString.expected

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
| DefaultToString.cs:9:27:9:27 | access to local variable d | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | DefaultToString.cs:4:7:4:21 | DefaultToString | DefaultToString |
2-
| DefaultToString.cs:10:28:10:28 | access to local variable d | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | DefaultToString.cs:4:7:4:21 | DefaultToString | DefaultToString |
1+
| DefaultToString.cs:9:27:9:27 | access to local variable d | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | DefaultToString.cs:4:14:4:28 | DefaultToString | DefaultToString |
2+
| DefaultToString.cs:10:28:10:28 | access to local variable d | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | DefaultToString.cs:4:14:4:28 | DefaultToString | DefaultToString |
33
| DefaultToString.cs:16:27:16:30 | access to local variable ints | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | | Int32[] | Int32[] |
44
| DefaultToString.cs:19:24:19:27 | access to local variable ints | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | | Int32[] | Int32[] |
5+
| DefaultToString.cs:31:27:31:27 | access to local variable f | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | DefaultToString.cs:59:23:59:30 | IPrivate | IPrivate |
56
| DefaultToStringBad.cs:8:35:8:35 | access to local variable p | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | DefaultToStringBad.cs:14:11:14:16 | Person | Person |
67
| DefaultToStringBad.cs:11:38:11:41 | access to local variable ints | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | | Int32[] | Int32[] |
78
| WriteLineArray.cs:7:23:7:26 | access to parameter args | Default 'ToString()': $@ inherits 'ToString()' from 'Object', and so is not suitable for printing. | | String[] | String[] |

0 commit comments

Comments
 (0)