Git Product home page Git Product logo

zerowaste's Introduction

Zerowaste

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

Introduction

In purely functional programming, expressions are always pure - their evaluation has no side effects. Side effects are therefore expressed with an IO-like type (e.g. Cats Effect IO) and they only happen when the IO is run - preferably with a single, "impure" invocation hidden somewhere in library code.

Because of that, in pure FP it never makes sense to discard the result of an expression, e.g.

val number = {
  discardedExpression // pointless!
  42
}

This is an easy mistake to make and it may cause very tricky bugs, e.g. when the discarded expression was an IO that did something important. The Scala compiler does not detect this kind of mistake because Scala is not a purely functional language so the compiler cannot assume that all expressions are pure.

This plugin reports a warning for any discarded expression whose type is not 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
}

Compatibility

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

Compiler plugins must be cross-built for every minor and patch version of Scala. If zerowaste is not available for your Scala version, please file an issue or submit a PR.

How to submit a PR with 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

zerowaste's People

Contributors

ghik avatar

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.