Skip to content

Unnecessary uint160(bytes20(b)) casting #16129

@pcaversaccio

Description

@pcaversaccio

So you state here:

Explicit conversions to and from address are allowed for uint160, integer literals, bytes20 and contract types.

Later below, in the "Warning" section you state:

You can use address(uint160(bytes20(b)))...

I also have seen the address(uint160(bytes20(...))) patterns many times in codebases. However, the bytes20 to uint160 cast is unnecessary as you even state in your docs that you can explicitly convert from bytes20 to an address. You can simply test this:

➜ bytes32 a = keccak256("hello")
➜ address(uint160(bytes20(a)))
Type: address
└ Data: 0x1C8AFf950685C2Ed4BC3174F3472287b56D9517b
➜ address(bytes20(a))
Type: address
└ Data: 0x1C8AFf950685C2Ed4BC3174F3472287b56D9517b
➜ payable(address(bytes20(a)))
Type: address
└ Data: 0x1C8AFf950685C2Ed4BC3174F3472287b56D9517b

Patched here: #16132.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛documentation 📖low effortThere is not much implementation work to be done. The task is very easy or tiny.low impactChanges are not very noticeable or potential benefits are limited.must haveSomething we consider an essential part of Solidity 1.0.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions