Skip to content

Commit aa8986f

Browse files
committed
auto merge of rust-lang#87 : alexcrichton/cargo/toml-warnings, r=wycats
Closes rust-lang#27
2 parents e8912bf + a2aa2bf commit aa8986f

File tree

7 files changed

+104
-11
lines changed

7 files changed

+104
-11
lines changed

libs/toml-rs

src/cargo/core/manifest.rs

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct Manifest {
2020
target_dir: Path,
2121
sources: Vec<SourceId>,
2222
build: Option<String>,
23+
unused_keys: Vec<String>,
2324
}
2425

2526
impl Show for Manifest {
@@ -192,6 +193,7 @@ impl Manifest {
192193
target_dir: target_dir.clone(),
193194
sources: sources,
194195
build: build,
196+
unused_keys: Vec::new(),
195197
}
196198
}
197199

@@ -234,6 +236,14 @@ impl Manifest {
234236
pub fn get_build<'a>(&'a self) -> Option<&'a str> {
235237
self.build.as_ref().map(|s| s.as_slice())
236238
}
239+
240+
pub fn add_unused_key(&mut self, s: String) {
241+
self.unused_keys.push(s)
242+
}
243+
244+
pub fn get_unused_keys<'a>(&'a self) -> &'a [String] {
245+
self.unused_keys.as_slice()
246+
}
237247
}
238248

239249
impl Target {

src/cargo/core/shell.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use term;
22
use term::{Terminal,color};
3-
use term::color::{Color, BLACK, RED, GREEN};
3+
use term::color::{Color, BLACK, RED, GREEN, YELLOW};
44
use term::attr::{Attr, Bold};
55
use std::io::{IoResult, stderr};
66
use std::fmt::Show;
@@ -63,6 +63,10 @@ impl MultiShell {
6363
pub fn error<T: ToStr>(&mut self, message: T) -> IoResult<()> {
6464
self.err().say(message, RED)
6565
}
66+
67+
pub fn warn<T: ToStr>(&mut self, message: T) -> IoResult<()> {
68+
self.err().say(message, YELLOW)
69+
}
6670
}
6771

6872
pub type ShellCallback<'a> = |&mut Shell|:'a -> IoResult<()>;

src/cargo/ops/cargo_compile.rs

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ pub fn compile(manifest_path: &Path, update: bool,
4343
let package = try!(source.get_root_package());
4444
debug!("loaded package; package={}", package);
4545

46+
for key in package.get_manifest().get_unused_keys().iter() {
47+
try!(shell.warn(format!("unused manifest key: {}", key)));
48+
}
49+
4650
let override_ids = try!(source_ids_from_config());
4751
let source_ids = package.get_source_ids();
4852

src/cargo/util/toml.rs

+39-8
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,37 @@ pub fn to_manifest(contents: &[u8],
2222
manifest\n\n{}", e)))
2323
};
2424

25-
toml_manifest.to_manifest(source_id).map_err(|err| {
25+
let pair = try!(toml_manifest.to_manifest(source_id).map_err(|err| {
2626
human(format!("Cargo.toml is not a valid manifest\n\n{}", err))
27-
})
27+
}));
28+
let (mut manifest, paths) = pair;
29+
match d.toml {
30+
Some(ref toml) => add_unused_keys(&mut manifest, toml, "".to_string()),
31+
None => {}
32+
}
33+
return Ok((manifest, paths));
34+
35+
fn add_unused_keys(m: &mut Manifest, toml: &toml::Value, key: String) {
36+
match *toml {
37+
toml::Table(ref table) => {
38+
for (k, v) in table.iter() {
39+
add_unused_keys(m, v, if key.len() == 0 {
40+
k.clone()
41+
} else {
42+
key + "." + k.as_slice()
43+
})
44+
}
45+
}
46+
toml::Array(ref arr) => {
47+
for v in arr.iter() {
48+
add_unused_keys(m, v, key.clone());
49+
}
50+
}
51+
_ => m.add_unused_key(key),
52+
}
53+
}
54+
55+
2856
}
2957

3058
pub fn parse(toml: &str, file: &str) -> CargoResult<toml::Table> {
@@ -126,9 +154,9 @@ impl TomlManifest {
126154
(Some(string.clone()), SourceId::for_central())
127155
},
128156
DetailedDep(ref details) => {
129-
let reference = details.branch.as_ref().map(|b| b.clone())
130-
.or_else(|| details.tag.as_ref().map(|t| t.clone()))
131-
.or_else(|| details.rev.as_ref().map(|t| t.clone()))
157+
let reference = details.branch.clone()
158+
.or_else(|| details.tag.clone())
159+
.or_else(|| details.rev.clone())
132160
.unwrap_or_else(|| "master".to_str());
133161

134162
let new_source_id = match details.git {
@@ -161,11 +189,14 @@ impl TomlManifest {
161189
}
162190

163191
let project = self.project.as_ref().or_else(|| self.package.as_ref());
164-
let project = try!(project.require(|| human("No `package` or `project` section found.")));
192+
let project = try!(project.require(|| {
193+
human("No `package` or `project` section found.")
194+
}));
165195

196+
let pkgid = try!(project.to_package_id(source_id.get_location()));
197+
let summary = Summary::new(&pkgid, deps.as_slice());
166198
Ok((Manifest::new(
167-
&Summary::new(&try!(project.to_package_id(source_id.get_location())),
168-
deps.as_slice()),
199+
&summary,
169200
targets.as_slice(),
170201
&Path::new("target"),
171202
sources,

tests/support/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ pub fn escape_path(p: &Path) -> String {
361361

362362
pub fn basic_bin_manifest(name: &str) -> String {
363363
format!(r#"
364-
[project]
364+
[package]
365365
366366
name = "{}"
367367
version = "0.5.0"

tests/test_cargo_compile.rs

+44
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,47 @@ test!(many_crate_types {
559559
assert!(file0.ends_with(os::consts::DLL_SUFFIX) ||
560560
file1.ends_with(os::consts::DLL_SUFFIX));
561561
})
562+
563+
test!(unused_keys {
564+
let mut p = project("foo");
565+
p = p
566+
.file("Cargo.toml", r#"
567+
[project]
568+
569+
name = "foo"
570+
version = "0.5.0"
571+
authors = ["[email protected]"]
572+
bulid = "foo"
573+
574+
[[lib]]
575+
576+
name = "foo"
577+
"#)
578+
.file("src/foo.rs", r#"
579+
pub fn foo() {}
580+
"#);
581+
assert_that(p.cargo_process("cargo-build"),
582+
execs().with_status(0)
583+
.with_stderr("unused manifest key: project.bulid\n"));
584+
585+
let mut p = project("bar");
586+
p = p
587+
.file("Cargo.toml", r#"
588+
[project]
589+
590+
name = "foo"
591+
version = "0.5.0"
592+
authors = ["[email protected]"]
593+
594+
[[lib]]
595+
596+
name = "foo"
597+
build = "foo"
598+
"#)
599+
.file("src/foo.rs", r#"
600+
pub fn foo() {}
601+
"#);
602+
assert_that(p.cargo_process("cargo-build"),
603+
execs().with_status(0)
604+
.with_stderr("unused manifest key: lib.build\n"));
605+
})

0 commit comments

Comments
 (0)