Giter VIP home page Giter VIP logo

ecocode-python's People

Contributors

dedece35 avatar utarwyn avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ecocode-python's Issues

EC4 - False positives with TypeVar

Describe the bug
When using python's TypeVar mechanism for type annotations, EC4 code smell is triggered.
This should not be considered a code smell since using it in global scope is the standard way.

To Reproduce
Example straight from the python official documentation
image

Expected behavior
Do not flag this as an error

Make "ecoCode-python" compatible with SonarQube 10.4 "DownloadOnlyWhenRequired" feature

Hello,

I'm Alex, Product Manager at Sonar.

SonarQube 10.4 will come with a feature to only download Sonar analyzers and 3rd-party plugins when they are really required by the Scanner. To make this feature work, each analyzer or 3rd-party plugin should declare the list of languages on which they expect to raise issues through a new MANIFEST property called Plugin-RequiredForLanguages.

See https://community.sonarsource.com/t/the-sonarscanners-download-only-required-3rd-party-plugins/108156 for more details.

Regards

SonarQube - plugin ecoCode Python - NullPointerException for rule AvoidMultipleIfElseStatementCheck

Describe the bug

During sonar scanning, each analyzed python file raises a NullPointerException :

WARN: Unable to analyze file: metadata/filters.py
java.lang.NullPointerException: null
	at fr.greencodeinitiative.python.checks.AvoidMultipleIfElseStatementCheck.computeElseVariables(AvoidMultipleIfElseStatementCheck.java:251)
	at fr.greencodeinitiative.python.checks.AvoidMultipleIfElseStatementCheck.visitElseNode(AvoidMultipleIfElseStatementCheck.java:238)
	at fr.greencodeinitiative.python.checks.AvoidMultipleIfElseStatementCheck.visitIfNode(AvoidMultipleIfElseStatementCheck.java:124)
	at fr.greencodeinitiative.python.checks.AvoidMultipleIfElseStatementCheck.visitNodeContent(AvoidMultipleIfElseStatementCheck.java:91)
	at fr.greencodeinitiative.python.checks.AvoidMultipleIfElseStatementCheck.visitNode(AvoidMultipleIfElseStatementCheck.java:74)
	at fr.greencodeinitiative.python.checks.AvoidMultipleIfElseStatementCheck.visitFuncDef(AvoidMultipleIfElseStatementCheck.java:63)
	at org.sonar.python.SubscriptionVisitor$SubscriptionContextImpl.execute(SubscriptionVisitor.java:110)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at org.sonar.python.SubscriptionVisitor.scan(SubscriptionVisitor.java:91)
	at org.sonar.python.SubscriptionVisitor.analyze(SubscriptionVisitor.java:71)
	at org.sonar.plugins.python.PythonScanner.scanFile(PythonScanner.java:139)
	at org.sonar.plugins.python.Scanner.execute(Scanner.java:59)
	at org.sonar.plugins.python.PythonSensor.execute(PythonSensor.java:118)
	at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:64)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:88)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:61)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:79)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:61)
	at org.sonar.scanner.scan.SpringModuleScanContainer.doAfterStart(SpringModuleScanContainer.java:82)
	at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
	at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
	at org.sonar.scanner.scan.SpringProjectScanContainer.scan(SpringProjectScanContainer.java:403)
	at org.sonar.scanner.scan.SpringProjectScanContainer.scanRecursively(SpringProjectScanContainer.java:399)
	at org.sonar.scanner.scan.SpringProjectScanContainer.doAfterStart(SpringProjectScanContainer.java:368)
	at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
	at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
	at org.sonar.scanner.bootstrap.SpringGlobalContainer.doAfterStart(SpringGlobalContainer.java:137)
	at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
	at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
	at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:71)
	at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:65)
	at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
	at com.sun.proxy.$Proxy0.execute(Unknown Source)
	at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
	at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
	at org.sonarsource.scanner.cli.Main.execute(Main.java:126)
	at org.sonarsource.scanner.cli.Main.execute(Main.java:81)
	at org.sonarsource.scanner.cli.Main.main(Main.java:62)

To Reproduce
Steps to reproduce the behavior:

# 1. run server
$ docker run -ti --rm -v sq_ecocode_logs:/opt/sonarqube/logs -v sq_ecocode_data:/opt/sonarqube/data -p 9000:9000 --name sonarqube-ecocode ghcr.io/green-code-initiative/sonarqube-ecocode:latest
# 2. go to http://localhost:9000, update admin password, create a project 'foo', then a token
token: sqp_439daaa786a5d68f243398fec2b9a54dc760cb7c
# 3. download plugin and add it to sonarqube
$ wget https://github.com/green-code-initiative/ecoCode-python/releases/download/1.4.0/ecocode-python-plugin-1.4.0.jar
$ docker cp ecocode-python-plugin-1.4.0.jar sonarqube-ecocode:/opt/sonarqube/extensions/plugins
$ docker restart sonarqube-ecocode
# 4. create a new Python Quality Profile with "eco-conception" rules, apply it on the project
# 5. run scanner
$ sudo docker run --network=host --rm -e SONAR_HOST_URL="http://localhost:9000" -e SONAR_SCANNER_OPTS="-Dsonar.projectKey=foo" -e SONAR_LOGIN="sqp_439daaa786a5d68f243398fec2b9a54dc760cb7c" -v "/foo_files:/usr/src" sonarsource/sonar-scanner-cli

Software Versions

  • SonarQube Version: Community Edition Version 10.1 (build 73491)
  • Plugin Version: ecocode-python-plugin-1.4.0.jar

!!! SonarQube crashes during starting !!!

Describe the bug
Sonarqube 10.3 crashes if plugin PYTHON 1.4.1 is installed (no problem if version 1.4.0)
Here are logs :
2024.01.10 13:38:59 INFO web[][o.s.s.r.r.RulesRegistrant] Register rules 2024.01.10 13:39:06 WARN web[][o.s.c.a.AnnotationConfigApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdk.internal.loader.ClassLoaders$AppClassLoader@75d0911a-org.sonar.server.rule.registration.RulesRegistrant': Initialization of bean failed; nested exception is java.lang.IllegalStateException: An incorrect state of deprecated rule keys has been detected.\n The deprecated rule key [gci-python:S34] was previously deprecated by [ecocode-python:EC34]. [ecocode-python:EC34] should be a deprecated key of [ecocode-python:EC35],\nThe deprecated rule key [gci-php:S34] was previously deprecated by [ecocode-php:EC34]. [ecocode-php:EC34] should be a deprecated key of [ecocode-php:EC35],2024.01.10 13:39:06 ERROR web[][o.s.s.p.Platform] Background initialization failed. Stopping SonarQube org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdk.internal.loader.ClassLoaders$AppClassLoader@75d0911a-org.sonar.server.rule.registration.RulesRegistrant': Initialization of bean failed; nested exception is java.lang.IllegalStateException: An incorrect state of deprecated rule keys has been detected. The deprecated rule key [gci-python:S34] was previously deprecated by [ecocode-python:EC34]. [ecocode-python:EC34] should be a deprecated key of [ecocode-python:EC35], The deprecated rule key [gci-php:S34] was previously deprecated by [ecocode-php:EC34]. [ecocode-php:EC34] should be a deprecated key of [ecocode-php:EC35], at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:628) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:920) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:200) at org.sonar.server.platform.platformlevel.PlatformLevel.start(PlatformLevel.java:80) at org.sonar.server.platform.platformlevel.PlatformLevelStartup.access$001(PlatformLevelStartup.java:58) at org.sonar.server.platform.platformlevel.PlatformLevelStartup$1.doPrivileged(PlatformLevelStartup.java:134) at org.sonar.server.user.DoPrivileged.execute(DoPrivileged.java:46) at org.sonar.server.platform.platformlevel.PlatformLevelStartup.start(PlatformLevelStartup.java:131) at org.sonar.server.platform.PlatformImpl.executeStartupTasks(PlatformImpl.java:201) at org.sonar.server.platform.PlatformImpl$AutoStarterRunnable.runIfNotAborted(PlatformImpl.java:362) at org.sonar.server.platform.PlatformImpl$1.doRun(PlatformImpl.java:116) at org.sonar.server.platform.PlatformImpl$AutoStarterRunnable.run(PlatformImpl.java:346) at java.base/java.lang.Thread.run(Unknown Source) Caused by: java.lang.IllegalStateException: An incorrect state of deprecated rule keys has been detected. The deprecated rule key [gci-python:S34] was previously deprecated by [ecocode-python:EC34]. [ecocode-python:EC34] should be a deprecated key of [ecocode-python:EC35], The deprecated rule key [gci-php:S34] was previously deprecated by [ecocode-php:EC34]. [ecocode-php:EC34] should be a deprecated key of [ecocode-php:EC35], at com.google.common.base.Preconditions.checkState(Preconditions.java:601) at org.sonar.server.rule.registration.RulesKeyVerifier.verifyRuleKeyConsistency(RulesKeyVerifier.java:70) at org.sonar.server.rule.registration.RulesRegistrant.start(RulesRegistrant.java:110) at org.sonar.core.platform.StartableBeanPostProcessor.postProcessBeforeInitialization(StartableBeanPostProcessor.java:33) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:440) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ... 19 common frames omitted 2024.01.10 13:39:06 INFO web[][o.s.p.ProcessEntryPoint] Hard stopping process 2024.01.10 13:39:06 INFO web[][o.s.s.n.NotificationDaemon] Notification service stopped 2024.01.10 13:39:06 INFO web[][c.z.h.HikariDataSource] HikariPool-1 - Shutdown initiated... 2024.01.10 13:39:06 INFO web[][c.z.h.HikariDataSource] HikariPool-1 - Shutdown completed. 2024.01.10 13:39:06 INFO app[][o.s.a.SchedulerImpl] Process[Web Server] is stopped 2024.01.10 13:39:06 WARN app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [ElasticSearch]: 143 2024.01.10 13:39:06 INFO app[][o.s.a.SchedulerImpl] Process[ElasticSearch] is stopped 2024.01.10 13:39:06 INFO app[][o.s.a.SchedulerImpl] SonarQube is stopped

Software Versions

  • SonarQube Version: 10.3
  • Plugin Version: ecoCode-python 1.4.1

Additional context
No problem with ecoCode-python 1.4.0

Rule EC7 detects object (__init__) with one argument as a setter

Rule EC7 (AvoidGettersAndSetters) detects __init__ methods with one argument and assignment as a setter.

To Reproduce
Code that can reproduce the error:

class Something:
    def __init__(self, value):
        self.value = value
...

Expected behavior
It shouldn't be detected as a code smell since init method is used to assign values to object properties.

Software Versions

  • SonarQube Version: 9.9
  • Plugin Version: 1.4.0

EC69 : Irrelevant rule for Python code

Rule EC69 is irrelevant for Python. The rule seems to imply that calling a function in a for statement results in calling this function multiple times, which is not true.

The two following syntaxes are strictly equivalent regarding the number of calls to the range builtin function :

for num in range(10):
    pass

iterable = range(10)
for num in iterable:
    pass

A simple way to prove see this is using a counter :

NUM_CALLS = 0


def func_to_iterable():
    global NUM_CALLS
    NUM_CALLS += 1
    return range(10)


def supposedly_ok_function():
    iterable = func_to_iterable()
    for num in iterable:
        pass


def supposedly_bad_function():
    for num in func_to_iterable():
        pass


if __name__ == '__main__':
    supposedly_bad_function()
    supposedly_ok_function()

    print(NUM_CALLS)

This prints 2 since the function is called only twice.

Therefore, I think the rule should be dropped since it's confusing and doesn't make much sense from an eco-design perspective, at least for Python, but it should be investigated for other languages too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.