EzDevInfo.com

scalastyle

scalastyle

How can I suppress Scalastyle warning

I got following code:

    string match {
      case Regex(_, "1", "0", _, _)    =>
      case Regex(_, "1", "1", null, _) =>
    }

Scalastyle is complaining about usage of null which cannot be avoided here. Any way I can suppress warning just for this line?


Source: (StackOverflow)

Reassign ArrayList in a better Way

The below code is working, but its looking odd to me, is there any better way to this.

var res:scala.collection.mutable.LinkedHashMap[String,scala.collection.immutable.Map[String,String]]=??
var arList = new ArrayList[String]()
res.keySet.map(arList.add(_))
//here res key set changed so i want to reassign the list by new keySet
res=??                                                        //updated
arList.clear
res.keySet.map(arList.add(_))

its looking very odd that to call the .clear on arList


Source: (StackOverflow)

Advertisements

What's the current state of static analysis tools for Scala?

I saw a StackOverflow question regarding static analysis in Scala, but that one was answered in 2009. As you know, the Scala tools are changing very rapidly.

I was therefore wondering if someone familiar with the current state of static analysis tools in Scala could tell me if there's, say, a Findbugs equivalent for Scala. I found that Findbugs issues many unnecessary warnings for Scala, probably having to do with the way the "object" singleton compiles to bytecode, due to traits, etc. I heard that Scalastyle is not only a Scala version of Java's CheckStyle, that it also includes bits of Findbugs and PMD. But if it doesn't implement all of Findbugs and/or PMD, then are there other tools that supplement it? Or, is Scalastyle good not only for style checking, but is it good for improving code quality?

Also, what about Scala's integration with, say, Sonar? Is the Scala Sonar plugin (which works with Scalastyle) reliable?


Source: (StackOverflow)

Scala "def" method declaration: Colon vs equals

I am at the early stages of learning Scala and I've noticed different ways to declare methods.

I've established that not using an equals sign makes the method a void method (returning a Unit instead of a value), and using an equals sign returns the actual value so

def product(x: Int, y: Int) {
  x*y
}

will return () (Unit), but

def product(x: Int, y: Int) = {
  x*y
}

will return the product of the two arguments(x*y)

I've noticed a third way of declaring methods - with a colon. Here is an example

def isEqual(x: Any): Boolean

How does this differ from the = notation? And in what situations will it be best to use this way instead?


Source: (StackOverflow)

Is there a better way to setup ScalaStyle under IntelliJ?

There's some scalastyle support in the Scala plugin for IntelliJ. This question is about the best way to set it up to pick up a scalastyle configuration file which is customarily at the root of the directory under the name scalastyle-config.xml (example for the Spark project).

Currently, to get the scalastyle plugin in IntelliJ to pickup our scalastyle config, you need to put a copy of it into your .idea folder (supposedly .project would also work, but I haven’t tested this). Also, the file needs to be named scalastyle_config.xml (with an underscore instead of the hyphen). In short, you need to call the following in the root of your project:

cp scalastyle-config.xml .idea/scalastyle_config.xml

While I think this is an ugly hack, it at least allows me to see the scalastyle violations before building the whole project. Please let me know if you have a better solution for this.


Source: (StackOverflow)

Use of scalastyleConfigUrl

I am very new to Scala and SBT

I am trying to set up the project with Scalastyle. All works fine when running from command line, however I can't find a way to define the option as indicated on the Scalastyle website http://www.scalastyle.org/sbt.html

I tried to add something like this in the plugins.sbt

val scalastyleConfigUrl = Some(url("http://www.scalastyle.org/scalastyle_config.xml"))

I am not sure how to validate if this is working; I would expect the scalastyle_config.xml to be downloaded at each compilation, obviously I am missing something.

Second part, I would like to automate scalastyle to run at each compilation/build. How can achieve that?

Thank you


Source: (StackOverflow)

Scalastyle "Public method must have explicit type" in Play Framework

We've started experimenting with Scala and the Play framework at my work. Setup our auto-linting and testing framework as the first thing, and have deployed Scalastyle to handle the former.

That has been very useful, except that we are getting this specific lint error that we are finding it difficult to resolve in a good way. A simple example is this:

  def helloWorld = Action {
    req =>
      Ok("Hello World!")
  }

Though often it can be much more complex, of course (to the point where it can difficult to figure out what the type actually is).

In either case, this gives us the "Public method must have explicit type" error from Scalastyle.

Unfortunately, setting the expected explicit type here seems typically to cause a syntax error.

Any suggestions on a good solution for this? Or do we just have to turn of this check for Play projects?


Source: (StackOverflow)

Is it possible to exclude sources from scalastyle with sbt?

I am using scalastyle with sbt and I would like to exclude some generated sources (e.g. generated by Slick schema generator). Is there any setting for that ?


Source: (StackOverflow)

Scala formatter - show named parameter

I have a relatively large Scala code base that does not use named parameters for any function/class calls. Rather than going in and manually entering it, which would be a very tedious process, I was looking at a formatter to do the job. The best I found is scalariform, but I'm not sure whether I can even write a rule for something so complex.

I'm curious if anyone has ran into a similar problem and found a powerful formatter.


Source: (StackOverflow)

Setting a third-party plugin setting in sbt AutoPlugin

I have an AutoPlugin which aggregates several third-party plugins and customizes their settings for our company. For most of the plugins, this works just fine by putting them in the projectSettings:

override lazy val projectSettings = Seq( somePluginSetting := "whatever" )

I tried to do this for ScalaStyle as well:

import org.scalastyle.sbt.ScalastylePlugin.scalastyleConfigUrl

override lazy val projectSettings = Seq(
  scalastyleConfigUrl := Some(url("http://git.repo/scalastyle-config.xml"))
)

This setting is never visible in projects using my plugin, instead sbt uses the plugin-provided default value:

> inspect scalastyleConfigUrl
[info] Setting: scala.Option[java.net.URL] = None
[info] Description:
[info]  Scalastyle configuration file as a URL
[info] Provided by:
[info]  {file:/Users/kaeser/Documents/workspace/ci-test-project/}root/*:scalastyleConfigUrl
[info] Defined at:
[info]  (org.scalastyle.sbt.ScalastylePlugin) Plugin.scala:101
[info] Delegates:
[info]  *:scalastyleConfigUrl
[info]  {.}/*:scalastyleConfigUrl
[info]  */*:scalastyleConfigUrl
[info] Related:
[info]  test:scalastyleConfigUrl

When I put the setting into build.sbt directly, it works as expected.

I made a simple example sbt plugin that shows the problem: https://github.com/jastice/sbt-customsettings

What might the issue be?


Source: (StackOverflow)

scalastyle producing empty output

I've been working on adding scalastyle to my scala project to check for potential problems in the code. But I keep getting empty results in the scalastyle-result xml file.

I've followed the steps from the scalastyle.org website. In summary here's what I did:

1- add the following to plugins.sbt

addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.3.1")

resolvers += "sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases/"

2- add the following line in build.sbt

org.scalastyle.sbt.ScalastylePlugin.Settings

3- add the file scalastyle-config.xml to the root directory of the project.

So now when I go to the sbt console, I can run "scalastyle" and I get the output file "scalastyle-result.xml" in ./target.

BUT the file only contains this:

<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="5.0"></checkstyle>

So basically it is not raising any warnings. Now no need to mention that I do have a few classes in my project. But I am unable to distinguish whether the results xml is valid or not. I assume there should at least be a few warnings.

I've previously done some work with checkstyle which is quite similar, and in the ant target I got to specify the directory that checkstyle should be looking into. Is it similar with scalastyle?

Thanks a lot for the help.


Source: (StackOverflow)

File line length exceeds 100 characters

I meet a problem when I compile spark version 1.3.1. When I compiled the original source codes provided by spark,it was OK. But when I added some source files into the mllib, it came up with errors,like:

  • message=File line length exceeds 100 characters

Based on the information at the end of compiling

  • [ERROR] Failed to execute goal org.scalastyle:scalastyle-maven-plugin:0.4.0:check (default) on project spark-mllib_2.10: Failed during scalastyle execution: You have 53 Scalastyle violation(s). -> [Help 1]

It should be because of the scalastyle test. I could finish my compile process by closing the validation of scalastyle.

But is there any other ways to handle this problem? I don't think just closing the validation is good enough

Example code of Errors:
good one

val implicitPrefs =  
    new BooleanParam(this, "implicitPrefs", "whether to use implicit preference", Some(false))

bad one

val implicitPrefs = new BooleanParam(this, "implicitPrefs", "whether to use implicit preference", Some(false))

Source: (StackOverflow)

How check naming convention for logger variables in scala with scalastyle?

Suppose I need to ensure that all loggers in my scala code are named log only, not LOGGER, LOG or logger. So for this style checking I need to define a Logger (for slf4j or log4j) and when style checker detects a variable with this type it check name.

I see org.scalastyle.scalariform.FieldNamesChecker, but do not see any type checkers, so is it possible to do that with scalastyle rules?


Source: (StackOverflow)

How to use the EnsureSingleSpaceAfterTokenChecker in scalastyle

I'm using the maven plugin for scalastyle.

The Scalastyle site lists EnsureSingleSpaceAfterTokenChecker as one of the new checkers, but doesn't give an example of how to use it.

I'm sure it's used like typical checkers, but I'm not sure how to configure the parameters.

Does anyone know how to configure it?

Is this legit?

<check enabled="true" class="org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker" level="warning">
    <parameters>
        <parameter name="token">for</parameter>
    </parameters>
</check>

Edit: What I have doesn't seem to be working. There's not error, but it doesn't catch a violation in my code.


Source: (StackOverflow)