Coder Social home page Coder Social logo

zerowaste's Introduction

Zerowaste

Scala compiler plugin to detect unused expressions (non-Unit).

Zerowaste is currently available for Scala 2.12.17+, 2.13.10+, 3.2.1+, 3.3.0+, and 3.4.0+

Compiler plugins must be cross-built for every minor and patch version of Scala. If zerowaste is not available for a Scala version that you want to use (most likely some freshly released one), please file an issue or submit a PR.

How to submit a PR with a new Scala version

  1. Add your desired Scala version to crossScalaVersions in build.sbt
  2. Run sbt githubWorkflowGenerate
  3. Commit the changes in build.sbt and github workflows
  4. Submit a PR

Introduction

Pure functional programming operates under the principle that expressions are free from side-effects. Side-effects are instead handled through an IO-like type, such as Cats Effect's IO, and are only executed upon explicit, unsafe runX invocation, usually hidden somewhere in library code.

As a consequence, discarding a result of an expression in purely functional code can always be assumed to be a mistake, e.g.

val number = {
  discardedExpression // pointless!
  42
}

This is an easy mistake and it can lead to tricky bugs, such as when an important IO action is unintentionally discarded. The Scala compiler cannot detect this issue as Scala is not a purely functional language and cannot assume all expressions are pure.

This plugin addresses this problem by reporting a warning for every discarded expression whose type is different than Unit.

Usage

Enable the plugin in build.sbt:

libraryDependencies += compilerPlugin("com.github.ghik" % "zerowaste" % "<version>" cross CrossVersion.full)

The plugin issues warnings, but it is often a good idea to turn them into compilation errors:

scalacOptions += "-Werror"

Note that these warnings, despite being converted to errors, can be suppressed with the @nowarn annotation:

import scala.annotation.nowarn

val number = {
  discardedExpression: @nowarn("msg=discarded expression")
  42
}

zerowaste's People

Contributors

alexander-klimov avatar bardurarantsson avatar ghik avatar kyri-petrou avatar mtomko avatar stanosphere avatar taig avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

zerowaste's Issues

Release for Scala 3.2.2

Scala 3.2.2 was released on Maven Central a few days ago (no github release yet) but I suspect it'll be released officially in the next 1-2 days.

I'm happy to create a PR for it, do I need to add 3.2.2 in build.sbt alongside 3.2.1 or replace it? I'm guessing it'd also have to be added to the build matrix in the .github/workflows/ci.yml file as well?

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.