diff --git a/swift-lang/src/main/java/io/ecocode/ios/swift/checks/sobriety/LocationLeakCheck.java b/swift-lang/src/main/java/io/ecocode/ios/swift/checks/sobriety/LocationLeakCheck.java index 0285074..9d2243a 100644 --- a/swift-lang/src/main/java/io/ecocode/ios/swift/checks/sobriety/LocationLeakCheck.java +++ b/swift-lang/src/main/java/io/ecocode/ios/swift/checks/sobriety/LocationLeakCheck.java @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package io.ecocode.ios.swift.checks.geolocalisation; +package io.ecocode.ios.swift.checks.sobriety; import io.ecocode.ios.swift.SwiftRuleCheck; import io.ecocode.ios.swift.antlr.generated.Swift5Parser; @@ -29,17 +29,15 @@ public class LocationLeakCheck extends SwiftRuleCheck { private static final String DEFAULT_ISSUE_MESSAGE = "calls must be carefully paired: CLLocationManager.startUpdatingLocation() and CLLocationManager.stopUpdatingLocation()"; protected boolean firstCallExist = false; protected boolean secondCallExist = false; - protected Swift5Parser.Postfix_expressionContext id; + protected Swift5Parser.ExpressionContext id; @Override public void apply(ParseTree tree) { - System.out.println(tree.getClass().getName()); - if (tree instanceof Swift5Parser.ExpressionContext && (tree.getText().contains(".startUpdatingLocation()"))) { firstCallExist = true; - id = (Swift5Parser.Postfix_expressionContext) tree; + id = (Swift5Parser.ExpressionContext) tree; } if (tree instanceof Swift5Parser.ExpressionContext && (tree.getText().contains(".stopUpdatingLocation()"))) { @@ -48,7 +46,6 @@ public void apply(ParseTree tree) { if (tree instanceof TerminalNodeImpl && tree.getText().equals("")) { if (firstCallExist && !secondCallExist) { - System.out.println("ok"); this.recordIssue(id.getStart().getStartIndex(), DEFAULT_ISSUE_MESSAGE); } firstCallExist = false; diff --git a/swift-lang/src/main/resources/ecocode_swift_profile.json b/swift-lang/src/main/resources/ecocode_swift_profile.json index 8e6eae7..223e369 100644 --- a/swift-lang/src/main/resources/ecocode_swift_profile.json +++ b/swift-lang/src/main/resources/ecocode_swift_profile.json @@ -10,6 +10,7 @@ "EC547", "EC523", "EC503", - "EC603" + "EC603", + "EC513" ] } diff --git a/swift-lang/src/main/resources/io/ecocode/rules/swift/EC513.html b/swift-lang/src/main/resources/io/ecocode/rules/swift/EC513.html new file mode 100644 index 0000000..106bc2e --- /dev/null +++ b/swift-lang/src/main/resources/io/ecocode/rules/swift/EC513.html @@ -0,0 +1,49 @@ +

Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. + In addition to these are the image sensor (a.k.a Camera) and the geopositioning sensor (a.k.a GPS).

+
The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor +unnecessarily process data when the app enters an idle state, typically when paused or stopped.
+Consequently, calls must be carefully pairwised: CLLocationManager.startUpdatingLocation() and CLLocationManager.stopUpdatingLocation().
+Failing to do so can drain the battery in just a few hours.

+

Noncompliant Code Example

+
+import CoreLocation
+
+class LocationTracker: NSObject, CLLocationManagerDelegate {
+    var locationManager: CLLocationManager?
+
+    override init() {
+        super.init()
+        locationManager = CLLocationManager()
+        locationManager?.delegate = self
+        locationManager?.requestAlwaysAuthorization() // Request appropriate authorization
+        locationManager?.startUpdatingLocation() // Start location updates
+    }
+
+    // LocationManager Delegate Methods
+    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
+        // Process new locations
+    }
+}
+
+
+

Compliant Code Example

+
+class LocationTracker: NSObject, CLLocationManagerDelegate {
+    var locationManager: CLLocationManager?
+
+    override init() {
+        super.init()
+        locationManager = CLLocationManager()
+        locationManager?.delegate = self
+        locationManager?.requestAlwaysAuthorization() // Request appropriate authorization
+        locationManager?.startUpdatingLocation() // Start location updates only when needed
+    }
+
+    // LocationManager Delegate Methods
+    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
+        // Process new locations
+        // Possibly stop updates if they are no longer needed
+        locationManager?.stopUpdatingLocation()
+    }
+}
+
\ No newline at end of file diff --git a/swift-lang/src/main/resources/io/ecocode/rules/swift/EC513.json b/swift-lang/src/main/resources/io/ecocode/rules/swift/EC513.json new file mode 100644 index 0000000..d152814 --- /dev/null +++ b/swift-lang/src/main/resources/io/ecocode/rules/swift/EC513.json @@ -0,0 +1,19 @@ +{ + "key": "EC513", + "title": "Leakage: Location Leak", + "defaultSeverity": "Major", + "description": "Most iOS devices come equipped with a variety of sensors that measure motion, orientation, and various environmental conditions. In addition to these, the devices include advanced sensors such as the image sensor (commonly referred to as the Camera) and the geo-positioning sensor (commonly referred to as GPS).\n\nThe common point of all these sensors is that they are power-intensive while in use. A typical issue arises when these sensors continue to process data unnecessarily after the application enters an idle state, typically when paused or when the user stops interacting with it.\n\nConsequently, calls must be carefully paired: CLLocationManager.startUpdatingLocation() and CLLocationManager.stopUpdatingLocation(). Failing to do so can drain the battery in just a few hours.", + "status": "ready", + "remediation": { + "func": "Constant/Issue", + "constantCost": "20min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "eco-design" + ], + "type": "CODE_SMELL" + } + \ No newline at end of file diff --git a/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java b/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java index 2b88cee..646cdc8 100644 --- a/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java +++ b/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java @@ -54,8 +54,8 @@ public void testMetadata() { } @Test - public void testRegisteredRules() { - assertThat(repository.rules()).hasSize(10); + public void testRegistredRules() { + assertThat(repository.rules()).hasSize(11); } @Test diff --git a/swift-lang/src/test/java/io/ecocode/ios/swift/checks/sobriety/LocationLeakCheckTest.java b/swift-lang/src/test/java/io/ecocode/ios/swift/checks/sobriety/LocationLeakCheckTest.java index 0f5825d..d23134e 100644 --- a/swift-lang/src/test/java/io/ecocode/ios/swift/checks/sobriety/LocationLeakCheckTest.java +++ b/swift-lang/src/test/java/io/ecocode/ios/swift/checks/sobriety/LocationLeakCheckTest.java @@ -39,7 +39,7 @@ public void locationLeakCheck_missing_release_trigger(){ SensorContextTester context = CheckTestHelper.analyzeTestFile(TEST_CASE_MISSING_RELEASE_CALL); ObjectAssert issue = assertThat(context.allIssues()).hasSize(1) .first(); - issue.extracting(Issue::ruleKey).isEqualTo("EC513"); + issue.extracting(Issue::ruleKey).extracting(RuleKey::rule).isEqualTo("EC513"); issue.extracting(Issue::ruleKey).extracting(RuleKey::repository) .isEqualTo("ecoCode-swift"); issue.extracting(Issue::primaryLocation)