New issue
Advanced search Search tips

Issue 691787 link

Starred by 2 users

Issue metadata

Status: Assigned
Owner:
Cc:
EstimatedDays: ----
NextAction: ----
OS: All
Pri: 2
Type: Feature



Sign in to add a comment

Add a check for const value function parameter

Project Member Reported by vmp...@chromium.org, Feb 13 2017

Issue description

We should prevent accidental use of const value function parameters (eg void foo(const Bar bar)) when the user meant to use a const reference instead. Although valid, there is limited value to allowing const value parameters, and the inefficiencies that result from accidental use can be significant.

from pkasting@:
"""
There is one total time I have ever run into "const T" as an arg where it turned out to be intentional and not a typo, and in that case it wasn't actually preventing an error, the coder in question was just even more zealous than me about using const everywhere.  Whereas unintentionally typoing "const T&" this way has been an error I've seen dozens and dozens of times in Chromium.

This is why I purposefully did not propose a limited check in the form of warning about move-from-const, or warning only on "const non-BT".  The pattern has such low benefit that it's less confusing and no more harmful to entirely ban it than to try to limit the check to cases where it's going to trigger real bugs.
"""

One question is whether to limit this to non-primitive types or do it for everything.

I'm going to do a bit more investigations to see how many instances in Chromium this check would affect.
 

Comment 1 by vmp...@chromium.org, Feb 13 2017

Labels: -Type-Bug Type-Feature

Comment 2 by vmp...@chromium.org, Feb 21 2017

I wrote a little check for const value in methods in Chromium. It found 859 instances in 452 files. There are 224 distinct types that are written as "const T". Some of these seem OK, such as

const char
const double
const enum values
const int{,32_t,64_t}
const size_t

Some however seem to be more questionable, such as

const std::vector<T>
const std::string
const std::set<T>
const std::map<K, V>
const scoped_refptr<T>

Project Member

Comment 3 by bugdroid1@chromium.org, Feb 22 2017

The following revision refers to this bug:
  https://chromium.googlesource.com/chromium/src.git/+/0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f

commit 0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f
Author: vmpstr <vmpstr@chromium.org>
Date: Wed Feb 22 20:09:02 2017

cc: Fix const value pattern by replacing it by non-const or reference.

This patch changes cc to ensure that we don't have a const value
parameters. These are replaced by either non-const value or by
reference to const depending on the situation.

R=danakj@chromium.org
BUG=691787
CQ_INCLUDE_TRYBOTS=master.tryserver.blink:linux_trusty_blink_rel

Review-Url: https://codereview.chromium.org/2710593003
Cr-Commit-Position: refs/heads/master@{#452177}

[modify] https://crrev.com/0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f/cc/animation/animation_host_perftest.cc
[modify] https://crrev.com/0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f/cc/debug/rasterize_and_record_benchmark_impl.cc
[modify] https://crrev.com/0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f/cc/input/page_scale_animation.h
[modify] https://crrev.com/0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f/cc/layers/scrollbar_layer_unittest.cc
[modify] https://crrev.com/0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f/cc/test/pixel_comparator.h
[modify] https://crrev.com/0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f/cc/tiles/prioritized_tile.cc
[modify] https://crrev.com/0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f/cc/tiles/prioritized_tile.h
[modify] https://crrev.com/0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f/cc/tiles/software_image_decode_cache.cc
[modify] https://crrev.com/0e5ce3e075b6bdde16328a4f280a1f8ba8621a2f/cc/trees/layer_tree_host_unittest_scroll.cc

Sign in to add a comment