Skip to content

Commit b485e2b

Browse files
committed
auto merge of rust-lang#10881 : sanxiyn/rust/allocation-lint-2, r=alexcrichton
2 parents 4e0cb31 + 3b14f25 commit b485e2b

File tree

6 files changed

+62
-23
lines changed

6 files changed

+62
-23
lines changed

src/librustc/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ pub fn build_link_meta(sess: Session,
428428
}
429429

430430
fn len_and_str_lit(l: ast::lit) -> ~str {
431-
len_and_str(pprust::lit_to_str(@l))
431+
len_and_str(pprust::lit_to_str(&l))
432432
}
433433

434434
let cmh_items = attr::sort_meta_items(cmh_items);

src/librustc/front/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn mk_test_module(cx: &TestCtxt) -> @ast::item {
330330
};
331331

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

335335
return @item;
336336
}

src/librustc/middle/lint.rs

+29-9
Original file line numberDiff line numberDiff line change
@@ -1028,27 +1028,47 @@ fn check_unused_mut_pat(cx: &Context, p: &ast::Pat) {
10281028
}
10291029
}
10301030

1031+
enum Allocation {
1032+
VectorAllocation,
1033+
BoxAllocation
1034+
}
1035+
10311036
fn check_unnecessary_allocation(cx: &Context, e: &ast::Expr) {
1032-
// Warn if string and vector literals with sigils are immediately borrowed.
1033-
// Those can have the sigil removed.
1034-
match e.node {
1037+
// Warn if string and vector literals with sigils, or boxing expressions,
1038+
// are immediately borrowed.
1039+
let allocation = match e.node {
10351040
ast::ExprVstore(e2, ast::ExprVstoreUniq) |
10361041
ast::ExprVstore(e2, ast::ExprVstoreBox) => {
10371042
match e2.node {
10381043
ast::ExprLit(@codemap::Spanned{node: ast::lit_str(..), ..}) |
1039-
ast::ExprVec(..) => {}
1044+
ast::ExprVec(..) => VectorAllocation,
10401045
_ => return
10411046
}
10421047
}
1048+
ast::ExprUnary(_, ast::UnUniq, _) |
1049+
ast::ExprUnary(_, ast::UnBox(..), _) => BoxAllocation,
10431050

10441051
_ => return
1045-
}
1052+
};
1053+
1054+
let report = |msg| {
1055+
cx.span_lint(unnecessary_allocation, e.span, msg);
1056+
};
10461057

10471058
match cx.tcx.adjustments.find_copy(&e.id) {
1048-
Some(@ty::AutoDerefRef(ty::AutoDerefRef {
1049-
autoref: Some(ty::AutoBorrowVec(..)), .. })) => {
1050-
cx.span_lint(unnecessary_allocation, e.span,
1051-
"unnecessary allocation, the sigil can be removed");
1059+
Some(@ty::AutoDerefRef(ty::AutoDerefRef { autoref, .. })) => {
1060+
match (allocation, autoref) {
1061+
(VectorAllocation, Some(ty::AutoBorrowVec(..))) => {
1062+
report("unnecessary allocation, the sigil can be removed");
1063+
}
1064+
(BoxAllocation, Some(ty::AutoPtr(_, ast::MutImmutable))) => {
1065+
report("unnecessary allocation, use & instead");
1066+
}
1067+
(BoxAllocation, Some(ty::AutoPtr(_, ast::MutMutable))) => {
1068+
report("unnecessary allocation, use &mut instead");
1069+
}
1070+
_ => ()
1071+
}
10521072
}
10531073

10541074
_ => ()

src/libsyntax/ext/quote.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -119,78 +119,78 @@ pub mod rt {
119119
impl<'self> ToSource for &'self str {
120120
fn to_source(&self) -> @str {
121121
let lit = dummy_spanned(ast::lit_str(self.to_managed(), ast::CookedStr));
122-
pprust::lit_to_str(@lit).to_managed()
122+
pprust::lit_to_str(&lit).to_managed()
123123
}
124124
}
125125

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

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

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

147147

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

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

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

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

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

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

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

src/libsyntax/print/pprust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1902,7 +1902,7 @@ pub fn print_meta_item(s: @ps, item: &ast::MetaItem) {
19021902
ast::MetaNameValue(name, value) => {
19031903
word_space(s, name);
19041904
word_space(s, "=");
1905-
print_literal(s, @value);
1905+
print_literal(s, &value);
19061906
}
19071907
ast::MetaList(name, ref items) => {
19081908
word(s.s, name);
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[deny(unnecessary_allocation)];
12+
13+
fn f(_: &int) {}
14+
fn g(_: &mut int) {}
15+
16+
fn main() {
17+
f(~1); //~ ERROR unnecessary allocation, use & instead
18+
g(~1); //~ ERROR unnecessary allocation, use &mut instead
19+
}

0 commit comments

Comments
 (0)