Skip to content

Commit 5c56796

Browse files
committed
Fixes handling of for loops
1 parent 1c15a9f commit 5c56796

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

src/metrics/loc.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -430,24 +430,31 @@ impl Loc for CppCode {
430430
}
431431
}
432432

433+
// impl fmt::Display for Java {
434+
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
435+
// write!(f, "{:?}", self)
436+
// }
437+
// }
438+
433439
impl Loc for JavaCode {
434440
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) {
435441
use Java::*;
436442

437443
let (start, end) = init(node, stats, is_func_space, is_unit);
438-
let kind_id = node.object().kind_id().into();
444+
let kind_id : Java = node.object().kind_id().into();
445+
//println!("KINDID {}", kind_id.to_string());
439446
match kind_id {
440447
Program => {}
441448
Comment => {
442449
add_cloc_lines(stats, start, end);
443450
}
444451
AssertStatement
445452
| BreakStatement
446-
| MethodInvocation
453+
| BinaryExpression
447454
| ContinueStatement
448455
| DoStatement
456+
| Declaration
449457
| ExpressionStatement
450-
| ForStatement
451458
| IfStatement
452459
| LocalVariableDeclaration
453460
| ReturnStatement
@@ -456,9 +463,21 @@ impl Loc for JavaCode {
456463
| TernaryExpression
457464
| ThrowStatement
458465
| TryStatement
466+
| UpdateExpression
459467
| WhileStatement => {
460468
stats.logical_lines += 1;
461469
}
470+
For => {
471+
if count_specific_ancestors!(
472+
node,
473+
ForStatement, Block
474+
) == 0
475+
{
476+
// handle for(int i:arr)
477+
// otherwise the statements in the for are counted elsewhere
478+
stats.logical_lines += 1;
479+
}
480+
}
462481
_ => {
463482
check_comment_ends_on_code_line(stats, start);
464483
stats.lines.insert(start);
@@ -1330,10 +1349,10 @@ mod tests {
13301349
}
13311350

13321351
#[test]
1333-
fn java_simple_lloc() {
1352+
fn java_for_lloc() {
13341353
check_metrics!(
1335-
"for (int i = 0; i < 100; i++) {
1336-
System.out.println(i);
1354+
"for (int i = 0; i < 100; i++) { // + 3
1355+
System.out.println(i); // + 1
13371356
}",
13381357
"foo.java",
13391358
JavaParser,
@@ -1344,13 +1363,30 @@ mod tests {
13441363
);
13451364
}
13461365

1366+
#[test]
1367+
fn java_foreach_lloc() {
1368+
check_metrics!(
1369+
"
1370+
int arr[]={12,13,14,44}; // +1
1371+
for (int i:arr) { // +1
1372+
System.out.println(i); // +1
1373+
}",
1374+
"foo.java",
1375+
JavaParser,
1376+
loc,
1377+
[
1378+
(lloc, 3, usize), // The number of statements is 3
1379+
]
1380+
);
1381+
}
1382+
13471383
#[test]
13481384
fn java_multi_lloc() {
13491385
check_metrics!(
1350-
"int max = 10;
1386+
"int max = 10; // +1
13511387
1352-
for (int i = 0; i < max; i++) {
1353-
System.out.println(i);
1388+
for (int i = 0; i < max; i++) { // +3
1389+
System.out.println(i); // +1
13541390
}",
13551391
"foo.java",
13561392
JavaParser,
@@ -1436,7 +1472,7 @@ mod tests {
14361472
14371473
class HelloWorldApp {
14381474
public void main(String[] args) {
1439-
System.out.println(\"Hello World!\"); // Display the string.
1475+
System.out.println(\"Hello World!\"); // Display the string. Only statement. +1 lloc
14401476
}
14411477
}",
14421478
"foo.java",
@@ -1445,7 +1481,7 @@ mod tests {
14451481
[
14461482
(sloc, 11, usize), // The number of lines is 11
14471483
(ploc, 6, usize), // The number of code lines is 5
1448-
(lloc, 2, usize), // The number of statements is 2
1484+
(lloc, 1, usize), // The number of statements is 1
14491485
(cloc, 5, usize), // The number of comments is 5
14501486
(blank, 1, usize) // The number of blank lines is 1
14511487
]

0 commit comments

Comments
 (0)