Skip to content

Extend allocation lint for boxing expressions #10881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ pub fn build_link_meta(sess: Session,
}

fn len_and_str_lit(l: ast::lit) -> ~str {
len_and_str(pprust::lit_to_str(@l))
len_and_str(pprust::lit_to_str(&l))
}

let cmh_items = attr::sort_meta_items(cmh_items);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/front/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn mk_test_module(cx: &TestCtxt) -> @ast::item {
};

debug!("Synthetic test module:\n{}\n",
pprust::item_to_str(@item.clone(), cx.sess.intr()));
pprust::item_to_str(&item, cx.sess.intr()));

return @item;
}
Expand Down
38 changes: 29 additions & 9 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,27 +1028,47 @@ fn check_unused_mut_pat(cx: &Context, p: &ast::Pat) {
}
}

enum Allocation {
VectorAllocation,
BoxAllocation
}

fn check_unnecessary_allocation(cx: &Context, e: &ast::Expr) {
// Warn if string and vector literals with sigils are immediately borrowed.
// Those can have the sigil removed.
match e.node {
// Warn if string and vector literals with sigils, or boxing expressions,
// are immediately borrowed.
let allocation = match e.node {
ast::ExprVstore(e2, ast::ExprVstoreUniq) |
ast::ExprVstore(e2, ast::ExprVstoreBox) => {
match e2.node {
ast::ExprLit(@codemap::Spanned{node: ast::lit_str(..), ..}) |
ast::ExprVec(..) => {}
ast::ExprVec(..) => VectorAllocation,
_ => return
}
}
ast::ExprUnary(_, ast::UnUniq, _) |
ast::ExprUnary(_, ast::UnBox(..), _) => BoxAllocation,

_ => return
}
};

let report = |msg| {
cx.span_lint(unnecessary_allocation, e.span, msg);
};

match cx.tcx.adjustments.find_copy(&e.id) {
Some(@ty::AutoDerefRef(ty::AutoDerefRef {
autoref: Some(ty::AutoBorrowVec(..)), .. })) => {
cx.span_lint(unnecessary_allocation, e.span,
"unnecessary allocation, the sigil can be removed");
Some(@ty::AutoDerefRef(ty::AutoDerefRef { autoref, .. })) => {
match (allocation, autoref) {
(VectorAllocation, Some(ty::AutoBorrowVec(..))) => {
report("unnecessary allocation, the sigil can be removed");
}
(BoxAllocation, Some(ty::AutoPtr(_, ast::MutImmutable))) => {
report("unnecessary allocation, use & instead");
}
(BoxAllocation, Some(ty::AutoPtr(_, ast::MutMutable))) => {
report("unnecessary allocation, use &mut instead");
}
_ => ()
}
}

_ => ()
Expand Down
22 changes: 11 additions & 11 deletions src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,78 +119,78 @@ pub mod rt {
impl<'self> ToSource for &'self str {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_str(self.to_managed(), ast::CookedStr));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}

impl ToSource for int {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}

impl ToSource for i8 {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i8));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}

impl ToSource for i16 {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i16));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}


impl ToSource for i32 {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i32));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}

impl ToSource for i64 {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i64));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}

impl ToSource for uint {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}

impl ToSource for u8 {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u8));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}

impl ToSource for u16 {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u16));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}

impl ToSource for u32 {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u32));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}

impl ToSource for u64 {
fn to_source(&self) -> @str {
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u64));
pprust::lit_to_str(@lit).to_managed()
pprust::lit_to_str(&lit).to_managed()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,7 @@ pub fn print_meta_item(s: @ps, item: &ast::MetaItem) {
ast::MetaNameValue(name, value) => {
word_space(s, name);
word_space(s, "=");
print_literal(s, @value);
print_literal(s, &value);
}
ast::MetaList(name, ref items) => {
word(s.s, name);
Expand Down
19 changes: 19 additions & 0 deletions src/test/compile-fail/lint-allocation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[deny(unnecessary_allocation)];

fn f(_: &int) {}
fn g(_: &mut int) {}

fn main() {
f(~1); //~ ERROR unnecessary allocation, use & instead
g(~1); //~ ERROR unnecessary allocation, use &mut instead
}